I'm getting 'false' for the first test and 'true' for the second test. I'm suppose to create a function that will evaluate to these values. I thought I executed my code in the function correctly but I'm not getting what I'm suppose to be getting. I don't understand what I could be doing wrong here. Any help will be so appreciated. Thanks.
// - Given the arrayOfNames, determine if the array contains the given name. // - If the array contains the name, return true. If it does not, return false. // - Hint: Use a loop to "iterate" through the array, checking if each name matches the name parameter. // // Write your code here 👇 function contains(arrayOfNames, name) { for (index = 0; index < arrayOfNames.length; index++) { if (arrayOfNames[index] === name) { return true; } else { return false; } } } // -------TESTS--------------------------------------------------------------- // Run these commands to make sure you did it right. They should all be true. console.log("-----Tests for Exercise Five-----"); console.log("* Returns true when the array contains the name."); console.log(contains(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"], "nancy") === true); console.log("* Returns false when the name is not in the array"); console.log(contains(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"], "fred") === false);
https://stackoverflow.com/questions/67030103/javascript-function-to-determine-if-an-array-contains-a-value April 10, 2021 at 09:51AM
没有评论:
发表评论