How to search exactly for a name


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Actually it should print a word , letter for letter if the first letter is F and the 7th a k, but it doesn´t . HELP

/*jshint multistr:true */
var text= "Fredrik , do you Feel Free Fredrik? Do you feel fine, \
Fredrik? Or don´t  u feel so, Fred?";
var myName="Fredrik";
var hits = [];

for (var i=0 ; text.length >= i; i++) {
        if(text[i] === "F") {
            for(var j= i; j < (i + myName.length ); j++) {
                if ((i+ myName.length) === "k") {
                    hits.push(text[j]);
                }
                else {
                    break;
                }
            }
        }
}


console.log(hits);

#2
 if (text[i + myName.length -1]) === 'k')

However, this could generate false positives if other words have the same length and start with an ‘F’ and end with a ‘k’.


#3
function scanForName (name) {
  if (text.toLowerCase().indexOf(name.toLowerCase()) !== -1) {
    return name + ' was found in the text.';
  } else {
    return name + ' was not found in the text.';
  }
}

alert(scanForName('Fredrik'));

This function scans the text for ‘Fredrik’, and if it finds ‘Fredrik’ in the text, it will return that he was found. If not, it willl return that he was not.


#4

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