Let's Take a Trip explainer


#1

Aim of the puzzle: Use the let keyword to set the value of a variable.

Walk through of solution: There are 2 different variables both named ‘place’. var place is located outside the loop and can be used from inside or outside the loop. let place is defined inside the loop and can only be used inside the loop.

Tap inside the body {} of the loop and add a print() statement. Put the place variable inside the print().

You will see 5 lines print as output. The first line is place as defined with var outside the loop. It has the original value of ‘home’. The next 3 lines printed are from the loop. Inside the loop, place is defined using the keyword let which means it only exists inside this loop. The last line that prints is the original value of place as defined with var. This value never changed, what printed inside the loop was a different variable with the same name because it was defined with let instead of var.

Sample code solution:
(Tap below to reveal)

var place = 'home';
print(place);
for (let place of ['Algeria', 'Bangladesh', 'Cuba']) {
    print(place);
}
print(place);

Javascript Concepts: If Statements, For Loops, Identifiers, Arrays, Variables, Variable Scope

Grasshopper Concepts: print()


#5

Okay is wrong but i need to know why


#6

The output of your code will be correct, but you edited the list of countries to make it work. The goal of this puzzle is to see how a let variable is different from the var variable. You can change the value of the place inside the For Loop to lots of different values, but if you add another print(place) after the loop to see what the value of place is, you’ll see it will still be 'home'.

The let place in the For Loop is a separate variable from the var place in the 1st line. The let will only exist inside the block {} it is created – or the block of the For Loop if the let is created in the setup of a For Loop.

Here are the steps that the solution code is doing:

  • Crate the var place and set its value to 'home'
  • Print the value of var place which is 'home'
  • Create a let place which will loop through each country in the array (var place is still 'home')
  • Print the value of let place which will be 'Algeria', then 'Bangladesh', then 'Cuba' (var place is still 'home')
  • When the For Loop is finished, let place is gone
  • print the value of var place which is still 'home'

If you’ve created a var called place and a let called place in your code, and you use the identifier place, which place is being used? If you use place outside of the block {} that the let exists, then you’re using the var. If you use place inside of the block {} that the let exists, then you’re using the let.

var place = 'home'; // Only var exists
for (let place of listOfCountries) {
  // Both var and let exist, but place will use the let, and the var will not change
}
// Only var exists

–Frankie


#7


#8

You’ve changed the values stored inside the array to print() commands. You’ll want to reset the code back to just storing the strings inside the array. Then you can add a single command, print(place), inside the For Loop block {}.

–Frankie


#9

Silly me! Thanks Frankie!


#10

Why cant i make the for ( let of in the playground . It only allow for ( var of ?


#11

The code that the for...of button produces was created before we had lessons about let, and we wanted to include the variable declaration as part of the template. It’s not so simple to make it a blank and then allow the var or let key to be inserted there since we also include = __ when you create a variable. var element and let element can be used without the assignment operator = (we do that in the for...of loop). All that means is that the starting value of the variable will be undefined which is empty.

We’re hoping to have a future update where the for...of button will use a let or a var based on the solution code of a puzzle. In the playground, you shouldn’t have any problems as long as you don’t accidentally change the value of the For Loop’s var somewhere else in your code. But you’re right that it’s harder to practice with lets in the playground. Thanks for flagging this!

–Frankie


#12

Thank you. Another problem is that i cant open the keyboard in the playground. I hope to have a practical field so i can practice the fomular in a more simple , real and suitable for me. Real and free in short