7/7 how to do it with 'built-in' methods?


#1

My method is a bit dumb, it works though.

 var text="luo luoq luoqiqiqi luoqi lu";
var myName="luoqi";
var hits=[] ;

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

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

Well how do I utliize the built-in methods? Thx


#2

@luoqi,

You are aware that not using the curly-brackets-{ }
is a dangerous game

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

Have read through Crockford's recommendations
http://javascript.crockford.com/code.html


#3

Well the obvious solution would be to use these substring or substr methods you used in the early exercises. The more advanced way is probably to google about regular expressions.


#4

@leonhard_wettengmx_n
Thanks for the reminder.
Yeah, one of my other task was going wrong, and after i checked again and again, I found out that 2 lines of codes should execute within the "if" block, but i forgot to bracket them