Happy New Year explainer


#1

The aim of this puzzle: Edit the for loop to count down instead of up.
Walkthrough of the solution: This code uses a classic for loop which is a bit different from the for...of loop you might be used to. Instead of looping through an array or string, you tell the for loop how many times to repeat. It needs three pieces of information: a variable with a starting value, a test for continuing, and an update for each loop. We’ll take one part at a time. The first part is the starting value — since you want to count down from 10, your starting value should be 10. You also need to store this in a variable – it’s common to use i. The semicolons separate the parts of the for loop condition, so the second part is between the semicolons. The test is a check, just like in an if statement – if it’s true, the for loop will run the code inside the curly brackets, and if it’s false the for loop stops looping. Our for loop is trying to count from 10 to 1, and we don’t want it counting ‘0’ or into the negatives. That means the for loop should only run while i is positive, and you can write that with i > 0. (If you’re wondering why it’s not i > 1, that’s because when i = 1, i > 1 becomes 1 > 1 which is false, and the for loop will be over without ever printing the number 1). The third and final part of the for loop condition is the update — the update runs at the end of each loop. In this case, every time we loop we want the value of i to go down by 1. So, we want the update to be “the new value of i is the old value of i minus 1”. To write that in code is i = i - 1.
Sample code solution:
(Tap below to reveal)

for (var i = 10; i > 0; i = i - 1) {
  print(i);
}
print('Happy New Year!');

JavaScript Concepts: Code Block (for loop), Calling Functions, Variable Declaration, Identifiers, Loops
Grasshopper Concepts: print()


Whats wrong with my method?
#2

#3

#4

What confuses me is exactly why does it say happy New year when the counter finishes? From what I see the instruction at the bottom goes first, and it says to print happy New year, so why does it do a countdown first. Also what tells the code to finish the countdown before printing happy New year? Sorry I’m so new to this


#5

You don’t need to be sorry; it’s completely normal. We all have to start somewhere :slight_smile:
As for your questions, when we look at the code:

for (var i = 10; i > 0; i = i - 1) {
    print(i);
}
print('Happy New Year!');

Code will run from top to bottom, so we will start with the for loop. The counter will start at 10 (var i = 10) then go down by 1 (i = i - 1) until it reaches, but not including, 0 (i > 0).

Until i = 0, the for loop will do whatever is inside it’s curly brackets {} (print(i);) before returning back to the top of the for loop and removing 1 (i = i - 1).

Once i = 0, the loop will exit without executing the code inside and go to the next line after the for loop (print(‘Happy New Year!’);).

I hope that this helps! If something still sounds confusing (I have an issue with being vague and not realizing it) then feel free to reply back and I’ll do my best to clear things up! :smile:


#6

Oh so the for loop and the ‘print happy ny’ are separate commands. I was thinking they were all one command and that the system will go upwards instead of down looking for instructions. Thank you! You explained it perfectly. Just out of curiosity, how would you write a single code that says to print happy New year, but not if i had a value of i > 0. So like, it’s gonna say happy New year but it waits till the countdown finishes first?


#7

You could put 'Happy New Year' in the loop, but you’d need an if statement. Something like this would work:

for (var i = 10; i > 0; i = i - 1) {
    print(i)
    if (i === 1) {
        print('Happy New Year!')
   }
}

Since it requires nesting and more code, it’s normally (but not always) easier to have non-repeating parts outside of a loop.

H :slight_smile:


#8

It won’t let me put a semicolon. How do we add a condition if we don’t have that option?


#9

Hey @Sergius_South,

The semi-colons are added for you automatically! You don’t need to worry about syntax when coding with Grasshopper.

H :slight_smile:


#10

I didn’t use a string in my code so I think this helper message needs fixing. Great app overall!


#11

Hey @NewJerseyGuy,

Thanks for flagging this! It does look like a bug — I’m looking into now to determine the cause.

To solve this puzzle, try changing i < 1, to be i > 0.

H :slight_smile:


#12

3rd one I’m stuck on.


#13

My opinion: I feel like the “instructions” are a step by step solution that are given before even looking at the “puzzle.” If it just said “The aim of this puzzle is to edit the for loop to count down instead of up.” It would be much better for teaching. That would be actually figuring out the puzzle. If needed, a hint could be revealed showing what to do, otherwise this is not making you think as to why the results are what they are.


#14

Screenshot_2018-04-27-08-49-46


#15

Hello,
You are on the right track, the code hint is actually trying to tell you something. You need to change the for loop a bit:

for (var i = 10; i < 1; i = i-1) {

The < becomes >

We will also change the 1 to a 0 so it can loop till i is 1.

for (var i = 10; i > 0; i = i-1) {
print(i);
}
print (‘Happy New Year!’);

Good luck :slight_smile:

Thanks,
Ahsen


#16

@Ahsen, there’s a small typo. You meant:
for (var i = 10; i > 0; i = i-1) {
print(i);
}
not:
for (var i = 10; 1 > 0; i = i-1) {
print(i);
}
1 is always greater than 0!
:wink:


#17

@James_Boblak Haha… yes my bad kids were wanting attention should double checked my work.


#18

Thanks for everybody, and special thanks for your kids!


#19

Why is it not printing i? Print {i}


#20

When using the print() function, you want to using parentheses rather than curly braces. This will be the case for all functions. Curly braces are useful for defining if/else/for/while etc. blocks which will have several lines of code and the computer will use {} to know when the block of code ends.

Hope this helps :grin: