I have an app platform which is developed on "Edge". And my app is built on eclipse. In javascript files of my code many times i have declared my variables with boolean. But to run app on platform i have to convert these boolean values to string like "true". Then only i can set it true. In a big code it is not good to convert boolean to string everywhere. So is it possible that wherever i am having boolean value it can detect and convert to string so that the app platform developed on Edge can understand it ?
Why would you try to print out boolean values in the first place? Check to see if value is boolean where you are alert(value);
For example,
var value = true; // Somewhere in code
if (value == true) { // Just check if value is true/false
alert("true");
} else {
alert("false");
}
Your approach is unnecessary and you are making the problem more complex then needs to be. There is no way to check if a value is boolean while the code is running, so you are going to have to change it everywhere you want alert("true"); Sadly, you can't hack this with a different approach.
Related
I have a function that checks whether a user is premium by checking its flags:
isUserPremium() {
return this.flags & Flags.PREMIUM; // returns true
}
Now let's say that I'd want another function, but this time to check whether the user is free, but using the same flag. I tried negating the returned value, but I'd like to know if there was a better way to do this.
isUserFree() {
return !(this.flags & Flags.PREMIUM); // returns false
}
There isn't a way of checking whether a flag is not set with a single operator. I can suggest using !isUserPremium() instead of isUserFree() later in the code - don't create functions that invert a value returned from another function. However, make sure that you don't rely on this for security. Everything that is executing in the browser can be easily manipulated.
Made a boolean variable "disableReplaceDate", used in if-statements. Have to save the variable somehow because the page reloads and I need to use it to "check a status" sort of, so it doesn't return to default every time the page reloads.
Then, to save myself a lot of time and trouble, because I can't send them in the URL as parameters(it's occupied already by a complicated java file which manipulates it all and I'm no good at java at all), I decided to use window.localStorage setItem, getItem and removeItem etc., so basically
var disableReplaceDate = false;
window.localStorage.setItem("dRD", false);
disableReplaceDate = window.localStorage.getItem("dRD");
But now the if-statement, which looked as follows:
if(disableReplaceDate == true){/*do some stuff*/}
didn't work anymore! But then when I changed it to
if(disableReplaceDate){/*do some stuff*/}
It starts working suddenly.
And if that wasn't uncanny enough, it won't do this for all if-statements using the variable, I tried changing them to (!disableReplaceDate) and such, but it doesn't make them work.
Why is this? And how do I solve it?
localStorage only stores strings. And "false" evaluates as true in a context where a boolean is needed (for example a if statement).
You should change
disableReplaceDate = window.localStorage.getItem("dRD");
to
disableReplaceDate = window.localStorage.getItem("dRD") === "true";
I think because the type of your variable is "string" see this
typeof(disableReplaceDate)
The output will be string
I think you need to test this way if(disableReplaceDate === "false")
I'm currently making a Mario-esque game with Javascript, or more precisely, CraftyJS.
I've looked here as reference,
and understand how to save values inputed by the user.
But what I want to accomplish is to save certain booleans(is that what they are called?) automatically, or when the player presses a save button or something.
For example, I have a dungeon called dungeon1, and I create a variable to represent whether or not the dungeon has been completed.
It's var dungeon1 = false; by default.
But when the player completes dungeon1, it changes to var dungeon1 = true;,
resulting in new additions to the world map, such as a portal to dungeon2(this is working fine).
What I want to save is this var dungeon1 = true; statement, so that when the user opens the game again, the dungeons completed will be loaded and the corresponding unlocks will be shown correctly. How would I do so?
Is there a way to say, make a save management file called save.js, then store booleans such as the one above once they become true?
You would just store the string version of the boolean that you want.
So store "true" and "false", and instead of checking just like if(variable) you'd have to check in the form of if(variable === "true"). Its a little derpy but you can see how it doesn't really add more complexity to your code.
If that type of if checking just offends your sensibilities, then in the code where you "get" it from the localStorage you could then recast it to a boolean.
var bool = (localStorage.getItem("myItem") === "true")
PS. you can setItem with booleans... it will just cast them to strings :D because most things it calls toString() on to store them if they aren't already strings.
So you can do:
enter code herelocalStorage.setItem("myItem", true)
Here is my code:
sessionStorage.loggedIn = true;
if (sessionStorage.loggedIn) {
alert('true');
}
else {
alert('false');
}
Simple enough. There must be some small thing I'm not understanding about how JavaScript is evaluating these expressions. When I put sessionStorage.loggedIn = false, the "false" alert shows correctly. However, when I change sessionStorage.loggedIn to true, the "false" alert still pops, even after clearing the session. What am I not getting right with this expression? It seems so simple, maybe I just need another pair of eyes on it.
Try to change your code to
sessionStorage.setItem('loggedIn',JSON.stringify(true));
if (JSON.parse(sessionStorage.getItem('loggedIn'))) {
alert('true');
}
else {
alert('false');
}
and it should work consistently across all major browsers.
The interface with the setItem/getItem methods is how the spec is written, so going that way is safer than using the shortcut of assigning properties. Also, sessionStorage, like localStorage is a textbased storage mechanism, and not meant for storing objects, so you need to wrap calls with JSON.parse and JSON.stringify to get the expected results across the board.
Be aware that JSON.parse doesn't always play nice with undefined/null values, so it might be wise to do some type checking first.
You can read the spec for the storage interface here
Keys and Values in a WebStorage object (sessionStorage) must be strings. If they are not strings they "should" be converted to strings in the browser's implementation when you assign to sessionStorage. If you evaluate against "true" or convert to boolean it will work fine.
https://code.google.com/p/sessionstorage/
http://www.w3schools.com/html/html5_webstorage.asp
So, I'm new to Javascript. In my code I have an image object that is declared like so:
<img src="me.png" id="changepic" alt="That's me!">
In my scripting code, I want to write a boolean statement that returns true when the src is "me.png" as it is in the picture and returns false if it is NOT "me.png". This boolean statement is located within the conditions of the if statement below:
function changeImage() {
if (document.getElementById("changepic").src === "me.png"){
document.getElementById("changepic").setAttribute("src", "me2.png");
}
}
I assigned the function changeImage to a button. The code for the button is written below:
<button type="button" onclick="changeImage()">Change Image</button>
For some reason, the boolean statement inside of the if statement always returns false, although the src value of the Element really is "me.png." I figured that this is a problem with how I'm writing the equality statement, but I don't know how to write an equality statement in javascript that would return true if the src value is "me.png" and return false if the src value is "me2.png". Any takers?
Change:
if (document.getElementById("changepic").src === "me.png"){
To:
if (document.getElementById("changepic").src.match(/me\.png$/)){
as .src will typically contain a full path to the image even though you don't specify it in the HTML.
The problem is not with your equality, its with
document.getElementById("changepic").src
which, is probably not returning what you think it is returning. Try a
console.log(document.getElementById("changepic").src)
and verify that it really is "me.png". I bet its something else.
Be aware that in js, === tests equality of both type and value, where as == will test equality more loosely; it is more forgiving. for example
2 === "2" // false because not the same type
2 == "2" // true because == tries to make the types lines up, then tests
"Relative URIs are resolved to full URIs using a base URI. [RFC1808], section 3, defines the normative algorithm for this process. For more information about base URIs, please consult the section on base URIs in the chapter on links."
From here: http://www.w3.org/TR/1999/REC-html401-19991224/types.html#type-uri
You should just look at the "right-most" characters when you compare.
For example
var mystr = document.getElementById("changepic").src
mystr.substr(len(mystr)-6,6) === "me.png"