I have been trying to solve this problem using an if/else statement but it is just not working. I don't know what I am doing wrong here.
Complete the function droids that accepts an array of strings and iterates through the array using a FOR loop. Update the variable result to "Found Droids!" if the array contains the string "Droids". Otherwise update the variable result to "These are not the droids you're looking for."
Below is the code I wrote using an if/else statement with a for loop
function droids(arr) {
let result = ""
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "Droids") {
let result = "Found Droids!"
return result;
} else {
let result = "These are not the droids you're looking for."
return result
}
}
}
const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"]
const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]
console.log(droids(starWars)) // should log: "Found Droids!"
console.log(droids(thrones)) // should log: "These are not the droids you're looking for."
Aucun commentaire:
Enregistrer un commentaire