Reputation: 21
this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.
Here is it:
var hello = false
function toggleSt(I, E)
{
if ((I == "activate") && (!E))
{
E = !E
alert("activated")
}
else if ((I == "disable") && (E))
{
E = !E
alert("disabled")
}
}
toggleSt("activate", hello)
alert(hello)
I pasted the code on JSFiddle,
Hello is still false.
Upvotes: 0
Views: 524
Reputation: 111062
You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.
var hello = false
function toggleSt(I)
{
if ((I == "activate") && (!hello))
{
hello = !hello
alert("activated")
}
else if ((I == "disable") && (hello))
{
hello = !hello
alert("disabled")
}
}
toggleSt("activate")
alert(hello)
Upvotes: 0
Reputation: 5198
Felix is right. Try:
var hello = false
function toggleSt(I)
{
if ((I == "activate") && (!hello))
{
hello = !hello;
alert("activated")
}
else if ((I == "disable") && (hello))
{
hello = !hello
alert("disabled")
}
}
toggleSt("activate");
alert(hello)
Upvotes: 1