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


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


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++) {



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.


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