Get Back Home explainer


#1

Aim of the puzzle: Reset the value of a variable after a loop changes it.

Walk through of solution: The place variable is created with var outside the for loop. The variable is used inside the loop which changes its value. After the loop, the original value of place has been lost and must be set again.

Add the place variable to the line beneath the for loop. Set place equal to a new string value. Enter the word ‘home’ for the string’s value. On the next line, add a print() statement with the variable place inside the parentheses to print the new value of place.

Sample code solution:
(Tap below to reveal)

var place = 'home';
print(place);
for (place of ['Ecuador', 'Fiji', 'Guinea']) {
    print(place);
}
place = 'home';
print(place);

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

Grasshopper Concepts: print()


#4

I’m stuck. this is what my code looks like.


#5

I’m stuck heres my code.


#6

Why we have written at 2nd last that
Place = ‘home’ ;


#7

@GabbyG @Mel @ELBO_D2i
The code inside of the For Loop block {} will run each time through the loop.

place = 'home';
print(place);

You are setting the value of the place variable to 'home' right before printing it each time. The block of the For Loop should only have print(place) which will display the current country you’re visiting (the current value of the place variable as it loops).


@Mel You also have print('place') inside of your array. You can remove that element.


@GabbyG @Mel @ELBO_D2i
When you run the starter code, you see this print:

home
Ecudor
Fiji
Guinea

The goal is to also print that you end up at home after printing out all the country names in the array. If you simply add print(place) at the very bottom of your code, outside of the For Loop block {}:

var place = 'home';
print(place);
for (place of ['Ecuador', 'Fiji', 'Guinea']) {
  print(place);
}
print(place);

You’ll get:

home
Ecuador
Fiji
Guinea
Guinea

This is because the most recent value stored inside of place is Guinea. Take a close look at the For Loop setup. It is using the same place variable to loop through the countries, not a separate variable like element. Remember that a variable is like a box that can hold an item. In other puzzles, a separate box was used to hold the item through the For Loop; in this puzzle, we’re using only 1 box the whole time. That means you need to change the value back to 'home' before printing the last time.

–Frankie


#9

The app wont let you write the code the way the code solution is set up to pass the level. The only way I am able to get close to getting it right is setting the code up like this and it still says it is in correct…I have sent feed back through the app and haven’t gotten a response


#10

In this puzzle, all the code you add should be at the very bottom – below all the other code. If you reset to the starter code and then tap in the blank space below the last curly bracket } you should be able to add the 2 lines you need: assign the value 'home' to the place variable, and then print(place).

–Frankie


#11

Thanks for the help Frankie!!