I wonder why my first code doesn't work and the second does. I thought they're basically the same.... Looks like var inside if statement doesn't seem to be processed as expected. Could anyone please clarify why it doesn't work?
var myFarm = ["chickens", "pigs", "cows", "horses", "ostriches"];
var letterCheck = myFarm[i].charAt(0);
for (var i = 0; i < myFarm.length; i++) {
console.log(i);
}
if (letterCheck === "c" || letterCheck === "o") {
alert("Starts with 'c' or 'o'!");
}
var myFarm = ["chickens", "pigs", "cows", "horses", "ostriches"];
var arrayLength = myFarm.length;
for (var j = 0; j < arrayLength; j++) {
console.log(myFarm[j]);
if (myFarm[j].charAt(0) === "c" || myFarm[j].charAt(0) === "o") {
alert("Starts with a c or an o!");
}
}
Aucun commentaire:
Enregistrer un commentaire