7. Extra Step for Fun - How can I separate my name?


#1


Finished this section, great lesson, very challenging. As of now my code prints JasonJasonJasonJason

How can I get it to print with the space so that its Jason Jason Jason Jason


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


var text = "Blahh blah blahh blah blah blah Jason \
blah blah blah Jason blah blah Jason blah blah \
blah blah blahh blah blah Jason jason";

var myName = "Jason";

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]);
        }
    }
};

console.log(hits.join(''));


#2

there are couple things you can do:
write another for loop to print out the hits, in a nice way

You can also make hits a string, and add text[j] to it. (use += for strings), and then after the second for loop, add a space so the names are separated by spaces, slice off the last additional space before printing

There are so many ways to solve this problem.


#3

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