//Hello, this is a code which took me some time to really understand while learning: it allows you to find your name in a text and spell it word by word as many times it is written in the text. I had to use 3 variables for text, myName and hits. hits (by the function .push) allows to move from letter to letter, till your name in the text won't be found anymore. Capital letters count. This is another course realized in Codecademy by Eric Weinstein.
var text ="gabriele gabriele Gabriele gabriele and Gabriele";
var myName = "Gabriele";
var hits = [];
for (var i=0;i<text.length;i++){
if (text[i]==="G"){
for (var j=i; j<(myName.length+i);j++){
hits.push(text[j]);} } }
if (hits===0){console.log("Your name wasn't found!");}
else {console.log(hits);}
No comments:
Post a Comment
Please, comment!