Solution to 7. Victory session using a String method?



Hi guys, Does anyone have a solution to this problem using a String object method? For example String.match() for regular expressions? I can't figure it out, but would be very interested to see how others do it.


/*jshint multistr:true */
var text = "Hi Nimai, \ welcome to this awesome coding challenge. We hope you understand and enjoy the content. \ Enjoy your day Nelly."

var myName = "Nimai"

var hits = [];

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


if (hits.length === 0) {
    console.log("Your name wasn't found");
} else {


If you want to get the exact output as taught in the exercise, that is, if there are n occurrences of myName in text, hits should be of length myName.length * n in the format - [1st letter, 2nd letter, ..., last letter, 1st letter, 2nd letter, ... , last letter, ... repeated n times]

The solution is:

Use the .match method to get the occurrences array, like this: ["Nimai", "Nimai", "Nimai"] Count the occurrences and store it in count.
Take an empty array answer.
.split myName into a single-letter array.
.concat xwith themyNamearraycount times.

Only peek at the code when you've tried my explanation above

var text = "Hi Nimai, \ welcome to Nimai awesome coding challenge. We hope you understand and enjoy the content. \ Enjoy your day Nimai.";

var myName = "Nimai", answer = [],
    myName2 = myName.split(""),
    count = text.match(new RegExp(myName, "g")).length;
while(count --> 0)
    answer = answer.concat(myName2);


Thanks for your assistance. Btw, what does --> mean in the while statement? I tried googling this but couldn't find an answer.

I assume it decrements count by 1 each time, and as long count is greater then 0 it will run.


It's no special operator, though it's called the "down-to operator" :wink:

That's just whitespace mess!

Try differentiating the different operators present in that expression. There are two of them.

Still didn't get my hint? Peek at the answer:
while(count-- > 0)


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