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
Related
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));
}
I have this code:
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || ''
);
}
In my HTML, I call out a user's device brand and model using:
<script>document.write(getURLParameter("brand") + (" ") + getURLParameter("name"))</script>
The only problem is that this is only working for certain mobile devices. When it doesn't work, it's simply left blank. Is there a way to add a default value if it's empty?
You can simply compare the values with
if(name === " " || name == null) {
//code
}
or
if(name !== " " || name != null) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || ''
);
}
I have a recusive function that is supposed to loop through a json object and output the expression. However, my recusion seems to be off because it's outputting field1 != '' AND field3 == '' when it should be outputting field1 != '' AND field2 == '' AND field3 == ''
I've tried a couple different things and the only way I can get it to work is by creating a global variable outstring instead of passing it to the function. Where am I off? When I step through it, i see a correct result but once the stack reverses, it start resetting outstring and then stack it back up again but leaves out the middle (field2).
JSFiddle
function buildString(json, outstring) {
var andor = json.condition;
for (var rule in json.rules) {
if (json.rules[rule].hasOwnProperty("condition")) {
buildString(json.rules[rule], outstring);
} else {
var field = json.rules[rule].id;
var operator = json.rules[rule].operator;
var value = json.rules[rule].value == null ? '' : json.rules[rule].value;
outstring += field + ' ' + operator + ' ' + value;
if (rule < json.rules.length - 1) {
outstring += ' ' + andor + ' ';
}
}
}
return outstring;
}
var jsonObj = {"condition":"AND","rules":[{"id":"field1","operator":"!= ''","value":null},{"condition":"AND","rules":[{"id":"field2","operator":"== ''","value":null}]},{"id":"field3","operator":"== ''","value":null}]};
$('#mydiv').text(buildString(jsonObj, ""));
The function has a return of a string.
When you call the function recursively from within itself, you aren't doing anything with the returned string from that instance, just calling the function which has nowhere to return to
Change:
if (json.rules[rule].hasOwnProperty("condition")) {
buildString(json.rules[rule], outstring);
}
To
if (json.rules[rule].hasOwnProperty("condition")) {
// include the returned value in concatenated string
outstring += buildString(json.rules[rule], outstring);
}
DEMO
Why so complicated?
function buildString(obj) {
return "condition" in obj?
obj.rules.map(buildString).join(" " + obj.condition + " "):
obj.id + " " + obj.operator + " " + string(obj.value);
}
//this problem occurs quite often, write a utility-function.
function string(v){ return v == null? "": String(v) }
Im getting the output that looks like this
" object Object is no longer available and has been removed from assignment#257"
Using the following code
if(typeof find_staff.staff != 'undefined') {
var staff = find_staff.staff;
staff = $.extend({}, staff.preferences, staff.staff);
if(typeof(staff[staff_id]) != 'undefined') {
loop_continue = true;
}
else {
$('#' + i).html('<p>' + staff + ' is no longer available and has been removed from assignment #' + booking_id + '</p>');
// Break from loop
loop_continue = false;
}
}
else {
$('#' + i).html('<p>' + staff + ' is no longer available and has been removed from #' + v.booking_id + '</p>');
// Break from loop
loop_continue = false;
}
It's pretty clear that staff is an object, as that's what $.extend returns
var staff = find_staff.staff;
staff = $.extend({}, staff.preferences, staff.staff);
It just so happens that the string representation of an object is [Object, object].
So doing '<p>' + staff + ' is no lo... concantenates the object with a string, effectively doing staff.toString() which gives you [Object, object]
console.log(index + ",\"" + array+ "\"");
This produces:
Name "Tree"
Name "Undefined"
Name "park"
How can I have an output for
Name "Tree"
Name
Name "park"
where if the array variable is undefined then not print it
Create a function for logging.
function logValue(index, value) {
var val = ('"' + value + '"') || "";
index = '"' + index + '"';
console.log(index + " " + val);
}
Then use it like so.
logValue(index, array);
var i;
for(i=0;i<array.length; i += 1){
if(typeof array[i] !== "undefined"){
console.log(i + " " + Name);
}
}
your array probably contains names or not so...
no need to write functions for this simple check.
console.log(index+(array?' "'+array+'"':''));
example
http://jsfiddle.net/3znzY/