"7. Victory!" Is this solution the best?


#1


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


Hello everyone!
I know it is not a big deal, but I feel that it's possible to do it in a different and better way... I am just curious. Is this the best way? Do I need to make those "toLowerCase" or is there any better solution? If you have any more suggestions or you find any problem, please tell me!
Thank you!


var text = "asfiih isif sfij fa sfhisf ssff Fabio you Fatio sf ggdsdg FABIO See you fabio";
var myNamePrompt = prompt("What's your name?");
var myName = myNamePrompt.toLowerCase();
var hits = [];

for ( var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]||(myName[0].toLowerCase())) {
        var preMatches = text.substring(i,(i+myName.length));
        var matches = preMatches.toLowerCase();
        if (matches === myName) {
            hits.push(preMatches);
        }
    }   
}

var stringHits = hits.join(", ");

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


#2

You can also try as a test to uses regular expressions to solve this?


#3

Oh, I don't know what regular expressions are but I will look into it! Thank you so much!


#4

its so much hardy , b i can't realize the code .


#5

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