4. Your "if" statement


#1

Hi All,

I'm on "Lesson 4. Your "if" statement" and even though I got it right I am confused about the:
[i]

in
if (text[i] ==="A");

... can someone quickly explain it to me?

var text = "my name is Amanda and I Amanda am \
learning JavaScript";
var myName = "Amanda";
var hits = [];
for(var i=0; i<text.length ; i++);
if (text[i] ==="A");


#2

Both of these constructs require code blocks { ... }, and will not have a semi-colon where you have one written.

for ( ) {
    if ( ) {

    }
}

#3

Ah, my question is about: [i]

My code is correct, though having a hard time wrapping my head around: [i]


#4

That is the index.

text = "Lorem ipsum";
console.log(text[0])                   // L
console.log(text.length);              // 11
console.log(text[text.length - 1]);    // m

Iterables in JS are zero-indexed so the first index is always zero.

Not as it is presented in your opening post. That is why I focused on the errors, first.


#5

Oh. So this is not correct? I received "Way to go! Start Next Lesson"...

/*jshint multistr:true */
var text = "my name is Amanda and I Amanda am \
learning JavaScript";
var myName = "Amanda";
var hits = [];
for(var i=0; i<text.length ; i++);
if (text[i] ==="A");

https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/4?curriculum_id=506324b3a7dffd00020bf661#


#6

That is a lenient lesson checker, obviously. Nothing we can do about that, but you can correct your code by following the example pattern I wrote earlier.