This exercise is flawed!


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++){

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


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!


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.


Nevermind, the next exercise mentions this flaw.


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