5. Your Second For Loop - How is the output supposed to look like?


#1


Hi,

I am really unsure what is supposed to be an output of this step. Below is my code and console output.

Thanks a lot for any help,
Cheers


var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget quam fringilla, iaculis justo vitae, pretium mauris. Fusce luctus nunc et maximus ultricies. Sed vitae tincidunt nisl. Nullam orci orci, volutpat nec accumsan nec, mattis eu lectus. Sed non dolor a nunc commodo venenatis aliquet ac dui. Fusce feugiat Fuske id nunc nec viverra. Donec a lectus a lorem pretium porttitor. Fusce nec sem nibh. Proin id pulvinar ligula. Phasellus scelerisque arcu et nunc dignissim, quis porttitor ipsum gravida. Cras tristique tincidunt mi sed imperdiet. \
Duis consequat metus sed ante elementum viverra. Mauris condimentum eu turpis et dignissim. Fusce vitae porta neque. Cras tincidunt imperdiet erat sit amet sollicitudin. Sed augue lectus, vehicula sodales interdum et, fermentum vitae massa. Etiam sit amet porta leo. In rhoncus at mauris nec consectetur. Curabitur volutpat augue diam, a ultrices justo viverra laoreet. Donec lobortis ex et pharetra efficitur. Etiam ullamcorper vel metus ut accumsan. Pellentesque placerat et sem in faucibus. Sed varius quam posuere tempus efficitur. Phasellus ut lorem et nisi consequat tristique vel nec lacus. Praesent ac tempus arcu, ut aliquet lacus. Sed sed libero id nunc efficitur interdum nec id quam. Praesent vitae maximus elit.";

var myName = "Fusce";
var testText = "";
/*
for (i=0;i<text.length;i++){
    if (text[i]===myName[0]){
        testText = "";
        for (j=i;j<(i+myName.length);j++){
            testText=testText+text[j];
            console.log(testText);
        };
        hits.push(testText);
        console.log(hits);
    };
};
*/

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

var hits = [];

Console v1 (the final run of it)

[ 'F', 'u', 's', 'c', 'e', 'F', 'u', 's', 'c', 'e', 'F', 'u', 's', 'k', 'e', 'F', 'u', 's', 'c', 'e' ]

Console v2 (the final run of it)

[ 'Fusce', 'Fusce', 'Fuske', 'Fusce', 'Fusce' ]


#2

In the second for loop, the for statement isn't right.

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

#3

Thanks for your advice. But are not those loops basically the same? (With the difference that I did not put "var" in front of "j" and I put the length statement in parenthesis)

I tried your loop and the exercise still does not resolve correctly. :disappointed_relieved:

( j=i ; j<(i+myName.length) ; j++)

(var j=i; j<myName.length+i; j++)

#4

Putting var isn't the point. You did this "j<(i+myName.length)", the i before myName.length but I just recognised that it is right. Sorry about that.

The issue is with the if statement. I recommend you simplify things:

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

Sorry I had to virtually rewrite your code. :grimacing:


#5

I just realised what was the problem with my code :smiley:, basicaly everything worked fine, but take a look at my last line:

var hits = [];

I inserted the code before the declaration, so the last line of the code deleted the whole array. Silly me.

Thanks for your time @bayoishola20 :slight_smile: .


#6

Okay! :slight_smile:


#7

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