if alerted yes marker won't set - javascript

I am wanting to make an if statement that has an alert function.
However if I don't use the localstorage function the alert box will keep popping up but changes the marker.
If if do use the localstorage function the marker won't set the color.
Any ideas on what to do?
All help is appreciated.
var alerted = localStorage.getItem('alerted') || '';
if (alerted != 'yes') {
if (value.squawk == "7500" || value.squawk == "7600" ||value.squawk == "7700") {
console.log(value.hex + " is squawking " + value.squawk);
alert(value.hex + " is squawking " + value.squawk + ". This is usually an error in transponder transmission please DO NOT alert the local authorities");
markers[value.hex].setIcon(squawkerror(value));
}
} else

Just check for alerted around the alert and if not set, alert and set it. Next time, no alert will be set and no stored value will be changed.
var alerted = localStorage.getItem('alerted') || false;
if( value.squawk == 7500 || value.squawk == 7600 ||value.squawk == 7700 ){
console.log(value.hex + " is squawking " + value.squawk);
if( !alerted ){
alert(value.hex + " is squawking " + value.squawk + ". This is usually an error in transponder transmission please DO NOT alert the local authorities");
localStorage.setItem('alerted', true);
}
markers[value.hex].setIcon(squawkerror(value));
}

Related

Parameter Checks as Null/Undefined Despite Console Showing a Value

I have a function that references things. For the purpose of fixing the bug, I've removed other case statements (they don't change nor fix the problem). I have also removed the return, because that's irrelevant at this stage too.
For some reason, say I pass an element in the DOM: Referencer('id', 'hello'), despite Chrome Console telling me that type = 'id' that if (type === null || "" || "undefined") fires every single time.
Here's a JSBin: https://output.jsbin.com/secuciyeko
function Referencer(type, value) {
// Standard Declaration
"use strict";
// Open Console Group
window.console.groupCollapsed("[Scriptbase.js]/[Referencer] # " + Scriptbase.Time());
// Log Status
window.console.info("[Process Started # " + Scriptbase.Time() + "]");
/* ------- Computation ------- */
// Local Variables
var a = null;
// Log Status
window.console.log("[Success # " + Scriptbase.Time() + "]: Checking for unusable values.");
// Value Validation
if (type === null || "" || "undefined") {
// Log Status
window.console.error("[Failure : " + Scriptbase.Time() + "] : Failure to look up '" + type + "' with the value '" + value + "'.");
// Close Console Group
window.console.groupEnd();
// Exit Method
return;
}
if (value === null || "" || "undefined") {
// Log Status
window.console.error("[Failure : " + Scriptbase.Time() + "] : Failure to look up '" + type + "' with the value '" + value + "'.");
// Close Console Group
window.console.groupEnd();
// Exit Method
return;
}
// Log Status
window.console.log("[Success : " + Scriptbase.Time() + "] : Looking up '" + type.toUpperCase() + "' with the value of '" + value + "'.");
// Look Up Value
switch (type) {
case "id":
// Variable Assignment
a = document.getElementById(value);
// Log Status
window.console.log("[Success : " + Scriptbase.Time() + "]: Found an DOM ID of '" + value + "'.");
// Break Case
break;
}
}
With this
if (type === null || "" || "undefined") {
You are saying:
if type is equal to null
or if empty string is true
or if string 'undefined' is true
You are not actually comparing type to '' or 'undefined'.
You can change to
if (!type) {
Then if it is empty, null or undefined it will go in the condition.
Read about
Truthy
Falsy

not getting to my else in jquery javascript

net mvc application and I am trying to do some validation when someone clicks a button. Here is the code.
function productVerify() {
var intQty = $("#txtQty").val();
var strItemName = $("#item_Name").val();
var strItemDescription = $("#item_Description").val();
var intItemID = $("#item_ID").val();
var intItemPrice = $("#item_Price").val();
var strImgUrl = $("item_ImgUrl").val();
var intQty = $("#txtQty").val();
if (intQty < 1) {
alert("You cannot put an item quantity of 0 in your cart");
return false;
}
else {
//post into cart
alert(strItemName + " " + strItemDescription + " " + intItemID + " " + intItemPrice + " " + strImgUrl + " " + intQty + " " + "I got this far.....! good job")
}
}
this works in jsfiddle but for some reason it does not fully work in my mvc application. it does work on the first if because if I put a 0 in my text box I get the first alert, but nothing happens on the else inside my mvc application. This one part seems so easy, but it is killing me any help would be appreciated.
make sure you using a number in your if statement
//if !num
if (parseInt(intQty) == NaN) {
alert("Please enter a number");
return false;
} else {
//if < 1
if (parseInt(intQty) < 1) {
alert("You cannot put an item quantity of 0 in your cart");
return false;
//if >= 1
} else {
//do something
}
}

JavaScript beginner variable confusion

Learning JS from a book, the exercise question was this:
Modify the code of Question 1 to request the times table to be displayed from the user; the code
should continue to request and display times tables until the user enters ‐1. Additionally, do a check
to make sure that the user is entering a valid number; if the number is not valid, ask the user to
re‐enter it.
This is the proposed solution:
function writeTimesTable(timesTable, timesByStart, timesByEnd) {
for (; timesByStart <= timesByEnd; timesByStart++) {
document.write(timesTable + " * " + timesByStart + " = " +
timesByStart * timesTable + "<br />");
}
}
var timesTable;
while ((timesTable = prompt("Enter the times table", -1)) != -1) {
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a " +
"valid number, please retry", -1);
}
if (timesTable == -1) {
break;
}
document.write("<br />The " + timesTable +
" times table<br />");
writeTimesTable(timesTable, 1, 12);
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 4: Question 2</title>
</head>
<body>
<script>
</script>
</body>
</html>
This is my code, which also runs with the same result, without != -1:
function writeTimesTable(timesTable, timesByStart, timesByEnd) {
for (; timesByStart <= timesByEnd; timesByStart++) {
document.write(timesTable + " * " + timesByStart + " = " +
timesByStart * timesTable + "<br />");
}
}
var timesTable;
while (timesTable = prompt("Enter the times table", -1)) {
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a " +
"valid number, please retry", -1);
}
if (timesTable == -1) {
break;
}
document.write("<br />The " + timesTable +
" times table<br />");
writeTimesTable(timesTable, 1, 15);
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 4: Question 2</title>
</head>
<body>
<script>
</script>
</body>
</html>
Why do I need != -1 parameter in the first while statement, since my code runs perfectly fine? Why is it there, what is it for?
The check for -1 is almost but not quite superfluous. It catches the conditions 'user canceled prompt' and 'user entered an empty string' which evaluates to false. In your version, this terminates the loop but the requirement is to terminate at user input '-1'.
If a while loop doesn't return anything, it will return as -1 (or false). In the case of the original example, I assume that the != -1 condition is there for example purposes only so it makes more sense to a beginner.
Let's say you were only wanting to terminate the while loop when the user entered -2. To do that, you would need to specify the != -2 condition in the loop, but -1 would still terminate the loop.
You're telling the browser/compiler to keep executing the code in the while loop until the user enters -1. When timesTable gets the value "-1" - that is, when the user enters "-1" - the while loop stops running.
// timesTable gets what the user enters in the prompt
// while timesTable is not equal to -1, execute the code in brackets
while ((timesTable = prompt("Enter the times table", -1)) != -1) {
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a " +
"valid number, please retry", -1);

addEventListener does not work on "textarea" or "body"

While I'm writing a detect() function for objects, which the user would get a console.log message when they either mouseover or click an element. I got stuck at adding onclick and onmouseover for two elements (or more, but I can't see) : textarea, and body.
The element that works well is label. When I click it or mouseover it, I get a console.log message.
Here is my Code :
function $count(string){
count = count + 1;
if(count <= 9999){
if(count <= 999){
if(count <= 99){
if(count <= 9){
$log("0000" + String(count) + " : " + string);
}else{
$log("000" + String(count) + " : " + string);
}
}else{
$log("00" + String(count) + " : " + string);
}
}else{
$log("0" + String(count) + " : " + string);
}
}else{
$log(String(count) + " : " + string);
}
}
function $detect(obj, type, script){
if(obj.addEventListener){
obj.addEventListener(type, script, false);
}else{
if(window.attachEvent){
window.attachEvent('on' + type, script);
}else{
console.log("Neither window.addEventListener nor window.attachEvent is working.");
}
}
}
Object.prototype.listen = function(){
if(typeof(this) == "object"){
if(this.constructor == Array){
for(var i = 0; i < this.length; i++){
$detect(this[i], 'click', $count(this[i] + " EVENT : onclick"));
$detect(this[i], 'mouseover', $count(this[i] + " EVENT : onmouseover"));
}
}else{
if(isElement(this)){
$detect(this, 'click', $count(this + " EVENT : onclick"));
$detect(this, 'mouseover', $count(this + " EVENT : onmouseover"));
}else{
console.log("ERROR : this.constructor is not 'Object' or 'Array'.");
return null;
}
}
}else{
console.log("ERROR : typeof(this) is not 'Object'.");
return null;
}
return this;
};
When I perform this action :
document.getElementsByTagName("textarea")[0].listen();
I was willing to receive a console.log when I click it or move the mouse above it. However, when I click the textarea, it doesn't produce the console.log.
I guess the problem is at the detect() or listen().
However I did not want to use element.onclick = function(){}; because it will cover the original function, and I cannot add another onclick to it, so using addEventListener should be better.
Where, or which part, should I change my code?
The problem is at the listen() part :
//...
$detect(this, "click", $count("blahblahblah");
I need to wrap $count("blahblahblah") in function(){ }.
That's the problem. That's all.

Inability to change more than one className

I'm coming here after a few hours of outstanding rage, anger, shock - perhaps just plain incredulity - at what's happened to me here.
I've attempted to create the simplest of functions in a lengthy script - get an element by Id, then change part of the class depending on a variable. Sounds easy right? Here's the relevant code:
{literal}
// check to see what kind of ban is this based on the ban_until and ban_at columns in table banlist from return data of /inc/classes/bans.class.php
var banduration = {/literal}{$ban.until - $ban.at}{literal};
if (banduration !== 1 || 0) {
var bantype = ((banduration < 0 || banduration > 3600) ? "ban" : "warning");
}
// replace all classnames with others in case this is a warning/ban, and END the script before it also changes modnames
if (bantype === "warning" || "ban") {
document.getElementbyId("modname_message_background").className = "common_background " + bantype + "_background";
document.getElementById("modname_message_bottomribbon").className = "common_bottomribbon " + bantype + "_bottomribbon";
document.getElementById("modname_message_letterbox").className = "common_letterbox " + bantype + "_letterbox";
document.getElementById("modname_message_modname").className = "common_modname " + bantype + "_modname";
document.getElementById("modname_message_servertime").className = "common_servertime " + bantype + "_servertime";
document.getElementById("modname_message_signature").className = "common_signature " + bantype + "_signature";
document.getElementById("modname_message_topribbon").className = "common_topribbon " + bantype + "_topribbon";
document.getElementById("modname_message_username").className = "common_username " + bantype + "_username";
}
This is fairly self-explanatory: This is in a Smarty template, and $ban.until is the unix time of a ban's end, and $ban.at is the unix time that it was applied, and so on and so on. But as I ran this script, which is designed to change the ban message depending on individual moderator ranks (later on but I digress) and the message type (message, warning, or ban). When I inserted this, only the first line was used. Agitated, I spent two hours reworking it multiple times in different ways to absolutely no avail. Furious with myself, I wrote this:
if (bantype == "warning" || "ban") {
var list = ["modname_message_background","modname_message_bottomribbon","modname_message_letterbox","modname_message_modname","modname_message_servertime","modname_message_signature","modname_message_topribbon","modname_message_username"];
var secondlist = ["background","bottomribbon","letterbox","modname","servertime","signature","topribbon","username"];
for (var i=0;i<list.length;i++) {
document.getElementById(list[i]).className = "common_" + secondlist[i] + " " + bantype + "_" + secondlist[i];
}
}
return;
This didn't work either. I was incredulous - defeated, I come here, pleading for the ludicrously simple mistake I had to have missed, because only something so simple can be such a nuisance.
I can confirm that variable banduration is working perfectly (using alert).
if (bantype === "warning" || "ban")
doesn't do what you think it does. It's equivalent to:
if ((bantype === "warning") || "ban")
which is always true, because "ban" is not false. It should be:
if (bantype === "warning" || bantype === "ban")
And:
if (banduration !== 1 || 0)
should be:
if (banduration !== 1 && banduration !== 0)

Categories