Unsure as to why this will not work


#1


I am trying to review what all i have learned so far, up to this point, but it's not working.
Anything I am missing?


var names = ["Colt", "Chris", "Mark"];

var checkNames = function(input) { 
    var name = input;
    for(i = 0; i < names.length; i++) {
        if(name === names[i].toString()) {
            console.log("That name already exist!);
        }else {
            console.log("Name added!");
        }
    }
}

var input = prompt("Enter your name:");

checkNames(input);


#2

Hi this line

console.log("That name already exist!);

you didn't close the " " (String)


#3

Gah... newb mistake! Thank you! It is working now without error but it is not breaking the for-loop once it checks to array!


#4

change all you console.log() with the return statements


#5

That makes sense! It still does not seem to be iterating through the array first and then checking.
When I type "Mark" into the prompt it does not say it already exist it says it added him. But he is already there in position [2] :disappointed_relieved: any ideas?

thank you for teaching!


#6

can you send me the link a of the lesson ..


#7

it is not a lesson. I am simply using the compiler to test what I have learned with teh code i posted above


#8

I don't know why.. it doesn't even loop because if you change the 0 with 1 to the for loop (fot (i =1..)it will only say

"That name already exist!"

if you type Chris and if you put i equal to 2 it will print it if you type Mark


I think know why is because the return statement make it loop once because after return something it doesn't run the for loop again it make it stop... and that's why when you put the console.log() it loop for all the three name.


#9

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