Variable scope exercise

Var x = 10;
If (x > 5) {
Let x = 1;
}else {
Let x = 0 ;

This code prints out 10 but I don’t understand how?
If x is greater than 5 than x”should be” 1, right?
If it is less than 5 x “should be” 0. Since X is 10 shouldn’t it print out a “5”?

If someone could explain this to me I oils greatly appreciate it.

1 Like

You used ‘‘let’’ not ‘‘var’’. ‘‘let’’ only works inside the code block(’’{ }’’) where it is find.So inside the if statement, x has been change form 10 to 1 but this value ‘‘1’’ stays inside the { }, so the ‘‘print(x);’’ is not using 1 but 10 as the value of x as for the ‘‘print()’’, x has not been change.
In order for 1 to be printed out, the ‘‘print(x)’’ should be inside the ‘’{ }’’ below the ‘‘let x = 1’’.


Good Evening

I need help on my account personal names, here it appears different as from my Gmail account.

Any Help?

I don’t know if we can change it, but if you want to edit it because you don’t want this name to be on you grasshopper certificates, don’t worry, you can modify your name on those when you get them.

You can change your name(I just discovered that).
Click on your picture at the top right, then on your name. After that click on summary and lastly on preferences at the top of the page.

Hey, @Michael_Edwards. I see a problem. Inside both “if” and “else” block, you are using let not var. let statement only works inside the code block so the output will be 10. Try replace let statement with var element.

Hope that helps!

1 Like

it does not work for me