8/9 last practice for loops


#1

here is my code:
for ( var = i = 100; i < 0; i =- 5) {
console .log (i);
}
it says I didn't log 100 to the console help!!!


#2

never mind I figured it out!


#3

additional = after var and additional space in console.log?


#4

I am not sure exactly what I did but i judt tinkered with it some


#5

How did you figure this out? Sharing = Caring.


#6

I had a look at the code to see if I understood it and could tweak it so it run correctly:

first off i went back to basics and began by looking at the hint section....

for (start; end; increment) {
// Do something!
}

Original code:

for ( var = i = 100; i < 0; i =- 5) {
console .log (i);
}

Output:
SyntaxError: Unexpected token =

first thing I noticed is what haxor 789 mentioned - additional equal sign ( = ) after var and the error message indicated this when I ran it in console window.

tweaking the code to get it operational:

step 1 ------ additional equals sign (=) removed ------

When I run the code after removing the additional equals sign ( = )

for ( var = i = 100; i < 0; i =- 5) {
console.log (i);
}

uh oh...another error message appears:

Output:
Oops, try again. Your loop didn't log 100 to the console!

I think (could be completely wrong though!) this message arises because once we have removed the additional equals ( = ) sign the var declaration becomes redundant

?? mmm......
var = i ?
Do we need to declare i ? in the template I don;t recall us declaring i
I could do with feedback on this aspect - following the for loop structure i tended not to declare the i.

step 2 ---removal of the 'var' declaration before the i ----

for (i = 100; i > 0; i =- 5) {
console.log (i);
}

Output:
Oops, try again. Your loop didn't log 100 to the console!

mmm??? Ok??? so I remove the 'var' declaration from in front of the 'i' and the same error message is still showing so what else is there in the code that might need tweaking?...

I look at the instructions again..

Break the for loop down into its three main bits.

a. The iterator i begins at 100.
b. We want the condition to let us print when i is greater than 0.
c. We want to count down by 5. You should use i -= 5.

for (i=100; i<0; i =- 5){

K. so looking at the instructions I think I can tick off (a) for (i=100;,,,,){
a. The iterator i begins at 100.

i think b may need a tweak too because when I read the code what I saw was the computer asking if i was less than zero ( i <0; ) but looking at the instructions it says:

b. We want the condition to let us print when i is greater than 0.

I think we need this to be greater than (>) zero because the iterator begins at 100 and we are counting down so will be reducing the iterator number of 100 by 5 every cycle of the loop.

step 3 ----- change the less than (<) sign the other way around

for ( var i = 100; i > 0; i =- 5) {
console.log (i);
}

Output:
100

No..! code almost worked! wait a minute not another error message!:
Oops, try again. Your loop didn't log 95 to the console!

inner voice: Crikey guv'nor, u ain't arf workin me to da bleedin bone 'ere!! if we ad a union i'd call us out on a strike!

Better look at the instructions again cos I def. ain't gettin it at all ...grrr..thought functions was bad

K. I know we can tick off (a) and (b)

a. The iterator i begins at 100. for (i = 100; ...) {
b. We want the condition to let us print when i is greater than 0. for (... i > 0; ...) {

but what the heck is wrong with (c)?
Its great can't u C?.....
I know...funE ain't FUNe if U don't laugh!

c. We want to count down by 5. *You should use **i -= 5.***

for (i=100; i>0; i =- 5) {
console.log (i);
}

So I use the minus equals sign (-=) which is same as x = x - y if want it to decrement down by 5!

Oh, K. the error msg was telling me Your loop didn't log 95 to the console!

Ah. I need to put the minus (-) sign to the other way around so it goes before the equals (=) sign not after it....

step 4 ----- swap the minus (-) sign to the other side of the equals (=) sign ...

for (i=100; i>0; i -= 5) {
console.log (i);
}

RUN the tweaked code....

// Write your very own for loop!
a.
The iterator i begins at 100.
(i=100;...){

b. We want the condition to let us print when i is greater than 0.
(... i>0;...)

c. We want to count down by 5. You should use i -= 5.
(... i-=5){

for (start; end; increment)
for (i=100; i>0; i-=5)
{
// Do something!
console.log(i)
}

for (i=100; i>0; i-=5)
{
console.log(i)
}

Output:
100
95
90
85
80
75
70
65
60
55
50
45
40
35
30
25
20
15
10
5

Finally, it worked!

But... blimey how pernickety was that eh!


#7

Wow really good explanation, shame on me that I missed most of the errors :blush:
Seems like sometimes I just do lazy evaluation (stop after the first error).
Just one thing if it asks for not declaring a variable: do not delete the var rather add one and if none is missing search for another error. Also it seems that some examples got at the wrong position still a great explanation keep it up :smile:


#8

thx. I was going to leave the 'var' declaration in but in this instance, I skipped it as I was following along with the template provided to get the solution and sort of understand what I was doing in the process. Its a pretty hard slog when you start out but is gradually falling into place as I progress along with the lessons. I think its day 7 for me now. This forum is really helpful and quite proactive we can share our code, vent our frustration and laugh at the errors we make along the way.


#9

Sharing >caring??? maybe?


#10

i>= 5 also works....


#11

Thank you so much for taking the time to explain this in the detail in which you did! I really helped my understanding and learning with this one!


#12

no probs and really glad it was of some help - though I must admit its a bit long winded hehe.

hope u enjoy the rest of the course as I did - i have since studied HTML part 1 on edX MOOC platform and actually understood what the lecturer was explaining thanks to this course on codecademy.

Now I am braving it - and trying to understand basics of R in Data Analysis with MIT on edX - only a few months ago I was studying that very same class you are currently doing which first introduced to JavaScript and ever since its led me on the path to want to at least get to grips with basics in a few different programming languages haha.