Lesson 4 your IF statement


#1

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

the code runs.... but im getting this

Oops, try again. Remember how to check the value of an element in an array? You simply do arrayName[i]. Check the Hint if you need hep

im lost here


var x = 'rico rico';

var name = 'rico';

var cek = [];

for (var i = 0; i < x.length; i++) {
    if (x[i] === 'r') 
{
    console.log('i got it');
     for (var j = i; j < (name.length + i); j++) {cek.push(x[j]);}
}
}

if (cek.length === 0) {console.log('no');} else {console.log(cek);}


#2

Try to use correct variable name.


In the Question, they have asked you to declare variable names as "text", "myName" and "hits". Below is the correct code to clear the level.

var text = 'rico rico';

var myName = 'rico';

var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === 'r') 
{
    console.log('i got it');
     for (var j = i; j < (myName.length + i); j++) {hits.push(text[j]);}
}
}

if (hits.length === 0) {console.log('no');} else {console.log(hits);}

Cheers!


#3

This will happen when we attempt to write our own instructions rather than follow the explicit instructions given. These lessons are quite firm on the expectations. Variable names are important.

Something to know about the variable name as it applies outside of any functions (in global scope). name is a property of the window (or global) object. It cannot be set by our programs since it identifies the current Window. Inside a function it is okay to use the variable, but not in window context.

var text = "This can be a large block of text, Rico. We can then \
spice it with Rico and extend it with more text, Rico. \
Rico understands, right?";
var myName = "Rico";
var hits = [];
if (text[i] === myName[0])

This line should be removed since it is not expected nor asked for.

 console.log('i got it');

The last line should read something like,

if (hits.length === 0) {
    console.log("Your name was not found.")
} else {
    console.log(hits);
}

#4

ah.... so no more "winging it" then.......... thank you for the explaination


#5

so i had it right then..... annoying but at least i caught it... thanks


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.