-1

I need help using the forEach method to solve the problem below. I am unsure as how I can check to see if the below arrays has the element "Droids". If so, how can I incorporate conditional statements to have the function return the correct string?

function droids(arr) {
  let result = '';
  arr.forEach(droids(arr))
  if (arr === 'Droids') {
    result = 'Found Droids';
  } else {
    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."
ikiK
  • 6,328
  • 4
  • 20
  • 40
Titan619
  • 11
  • 2
  • in place of `forEach` use `filter` – Vivek Bani Jun 13 '21 at 00:48
  • 1
    You can use [some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) to check if at least one element fulfills the condition. What are you trying with `arr.forEach(droids(arr)) `? –  Jun 13 '21 at 00:58
  • Also see why not to use return in forEach: https://stackoverflow.com/questions/34653612/what-does-return-keyword-mean-inside-foreach-function and your line `arr.forEach(droids(arr))` is wrong. Change it to `arr.forEach(item => { if (item === 'Droids') {` Run the snippet I made for you and see why from the error. – ikiK Jun 13 '21 at 01:01

2 Answers2

1

If you are for some reason insisting on using forEach, you can use it like this (note, this is NOT a recommended method):

function droids(arr) {
  let result = "These are not the droids you're looking for.";
  Object.assign([], arr) //clone array, because "break" will mutate it
    .forEach((val, index, array) =>
  {
    if (val === 'Droids') {
      result  = 'Found Droids';
      array.length = index + 1; //break;
    }
  });
  return result;
}

const starWars = ["Luke", "Droids", "Finn", "Rey", "Kylo"] 
 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."

Otherwise it's better use indexOf() instead:

function droids(arr) {
  return arr.indexOf("Droids") == -1 ? "These are not the droids you're looking for." : 'Found Droids';
}

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."
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • The question insists on using forEach. – Titan619 Jun 13 '21 at 01:19
  • @Titan619, wait, is this for school? In that case, remove the break line and instead of cloning line use this instead: `arr.forEach(val =>`, it was added for speed optimization ;) `forEach` is the slowest loop method, it's better avoid using it. – vanowm Jun 13 '21 at 01:25
0

If you move off the forEach() requirement - You can also use .includes() which is a newer method of determining if a string exists in an array. -link - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

This is the way I would do it - but note that IE typically does not support it. But then no one likes IE anyway :)

Also note: When comparing strings and characters, includes() is case-sensitive.

ps - thanks to @vanowm for the skeleton of the solution that I modified to suit my approach :)

function droids(arr) {
  return arr.includes("Droids") 
    ? "Found Droids" 
    : "These are not the droids you're looking for.";
}

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."

If you absolutely have to use forEach - then its a simple comparison of each item in the array to the target string. Note that you only update the result string if the string matches - if you update on both the matched and non matched string - any non matches after a match will revert the restult to the not-matching text.

function droids(arr) {
  let result = "These are not the droids you're looking for."
  
  arr.forEach(function(arrItem){
    if (arrItem === 'Droids') {
      result = 'Found Droids'
    }
  })
   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."
gavgrif
  • 15,194
  • 2
  • 25
  • 27