Totally stuck


#1


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

/*jshint multistr:true */
var text = "Apple also hopes to use only renewable energy in all its facilities, including stores and factories - it's currently at 96% renewable usage - as well as encouraging its supply chain to only use renewable energy and ensure all its products are supplied in 100% recycled paper packaging.";
var myName = "Vlasis";
var hits = [];

for (var i = 0; i<= text.length; i++) {
    if  (text[i] === myName[0]) {
        for (var j=0; j < i +myName.length; j++){
            hits.push(text[i+j]);
           for(var j = i; j < (myName.length + i); j++) {
            hits.push(text[j]);
              }
         
            }
        }
    }

i got the error message: "Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable. "


Replace this line with your code.


#2

why do you have 3 loops? 2 loops is enough, one loop to loop over text, and the second loop to push the letters of your name into the array once the first letter is found


#3

Like @stetim94 says you only need two loops, three overcomplicates things. Remove your first for and I bet it would work the same.


#4

why the first one?


#5

Oops meant the second one haha.


#6

when i remove the second for loop i got the warning "Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable. "


#7

can we see an updated version of your code?


#8

/*jshint multistr:true */
var text = "Apple also hopes to use only renewable energy in all its facilities, including stores and factories - it's currently at 96% renewable usage - as well as encouraging its supply chain to only use renewable energy and ensure all its products are supplied in 100% recycled paper packaging.";
var myName = "Vlasis";
var hits = [];

for (var i = 0; i<= text.length; i++) {
if (text[i] === myName[0]) {
for (var j=0; j < i +myName.length; j++){
hits.push(text[i+j]);

          }

        }

}

#9

you removed your third loop, not your second


#10

/*jshint multistr:true */
var text = "Apple also hopes to use only renewable energy in all its facilities, including stores and factories - it's currently at 96% renewable usage - as well as encouraging its supply chain to only use renewable energy and ensure all its products are supplied in 100% recycled paper packaging.";
var myName = "Vlasis";
var hits = [];

for (var i = 0; i<= text.length; i++) {
if (text[i] === myName[0]) {
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}

    }
}

i got the same message...


#11

Vlasis is not present in string stored in text variable, so it can't be found


#12

That was too obvious.:yum: Sorry for the mess...


#13

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