This exercise is flawed!


#1



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


My code runs fine, but this code itself is incorrect. As you can see my name is craig, if another name is put into the text string that starts with the same letter and is the same length. It picks it up as a hit and let's you pass. This exercise should be redesigned. Programmers need to take account for all possibilites.


text = "Horse, chadd, craig, craig, craig, krissy, hannah, hannah, krissy, kyle, ice cream, gucci, chadd"

myName ="craig"

hits = []

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

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


#2

The question mentions that capitalisation is important. So, for the purposes of the exercise, your name could be "Craig" instead of "craig". Then you could write:

if (text[i] === "C") {

instead of:

if (text[i] === myName[0]){

I hope this helps. Good luck!


#3

But capitalization wouldn't matter if the other name was capitalized too. It still only determined the name by the length and first letter. You would need to determine the name by the entire name to ensure it was the correct name. First initial and length aren't good enough metrics.


#4

Nevermind, the next exercise mentions this flaw.


#5

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