Maria
Maria

Reputation: 3525

How to modify a global variable inside of a function?

x = 10
function lol(){
    x = 5
}
function rofl(){
    alert(x)
}

In the rofl() function, how can i make the alert popup number 5? help me please I'm so noob trying to figure this out for 4 hours now :S

Upvotes: 0

Views: 123

Answers (2)

Josh Mein
Josh Mein

Reputation: 28625

You dont have to do anything other than call the functions. You have set them up properly, but if you dont call them then nothing will happen.

x = 10
lol();
rofl();

function lol(){
    x = 5
}
function rofl(){
    alert(x)
}

​Live DEMO

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359786

You're close. Really, really close.

The only problem is that you're not actually invoking either of the functions. This is all you're missing:1

lol();
rofl();

Working demo: http://jsfiddle.net/mattball/VsGWe


1 Well, that and some semicolons.

Upvotes: 4

Related Questions