Compiler doesn't work in Conclusion? For Loops. 13


#1

I wrote this code

var programmingLanguages = ["Java", "C++", "Python", "JavaScript"]

for (i = 3; i === 0; i--){
console.log("I'm learning " + programmingLanguages[i])
}

//the compiler did nothing when i clicked next.

is that because this is the conclusion?

is there a way to test to see if this works?


#2
1    var programmingLanguages = ["Java", "C++", "Python", "JavaScript"]
2    for (i = 3; i === 0; i--){
3             console.log("I'm learning " + programmingLanguages[i])
4    }

your FOR Loop test condition on line2 isn't correct, here's the syntax for a for loop
for (initialization; condition test; increment or decrement)

so, you'd have to declare a variable for the initialization: var i = 0

for the test condition. your loop will run as long as your test condition comes back true
the loop will keep running, until the test condition tests to a false
if it returns a false on the first test, it wont run at all, as is the case for your loop, if you had i===3, the loop would run once, and be decremented by i-- on the next run. for the test cases like these it would be better to have it test for a Greater Than < or Less Than >, imo.

hope that helps.


#3

Just a minor correction: It would not even run once. If i is 3 and i should be === 0 then the answer is clearly false the increment/decrement option is only run after a successful run of the loop.


#4

Thank you! Thans for your input. Not sure I fully understand it, but learning... I changed my code and it works and does what I want it to do now. Thank you

var programmingLanguages = ["Java", "C++", "Python", "JavaScript"]

for (var i = 3; i >= 0; i--){
console.log("I'm learning " + programmingLanguages[i])
}


#5

Trying to add an if else statement to this.

Why does this code below give me an unexpected token else error?

//code

var programmingLanguages = ["Java", "C++", "Python", "JavaScript"]
var likeProgrammingornot = prompt("Do you like programming? YES or NO?")

if (likeProgrammingornot === "Yes") {
for (var i = 3; i >= 0; i--) {
console.log("I'm learning " + programmingLanguages[i])
};

else confirm("Change your nasty attitude!");
}


#6

fixed it

//code

var programmingLanguages = ["Java", "C++", "Python", "JavaScript"]
var likeProgrammingornot = prompt("Do you like programming? YES or NO?")

if (likeProgrammingornot === "YES") {
for (var i = 3; i >= 0; i--) {
console.log("I'm learning " + programmingLanguages[i])
}}
else { confirm("Change your nasty attitude!");
}