I am having a lot of trouble with the syntax and application of forEach functions. Please help!
Recreate the function droids from the previous challenge, but instead of using a FOR loop, use the built-in forEach method.
Previous challenge:
function droids(arr) {
let result = '';
for (let str of arr) {
if (str === 'Droids') {
return 'Found Droids!';
}
}
return `These are not the droids you're looking for.`;
}
// Uncomment these to check your work!
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."
This is what I have:
function droids(arr) {
let result = "";
arr.forEach(item =>
console.log(result))
if (result = "Droids") {
return "Found Droids!"
} else {
return "These are not the droids you're looking for.";
}};