-4

The first console.log is supposed to display "Found Droids!" and the second is supposed to display "Not Found", but I get the same "Not Found" message for both. Not sure how to utilize forEach in this case.

function droids(arr) {
  let result = '';
  if (arr.forEach === "Droids") {
    result = "Found Droid!"
  } else {
    result = "Droid Not Found"
  }
  return result;
}

const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"]
const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]
console.log(droids(starWars))
console.log(droids(thrones))
Olian04
  • 6,480
  • 2
  • 27
  • 54
Parham
  • 214
  • 3
  • 12
  • 3
    `arr.forEach === "Droids"`? What is this comparison supposed to achieve? `forEach` is a function, not a "Droids" – UnholySheep Sep 25 '20 at 18:46
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Ivar Sep 25 '20 at 18:49
  • 1
    Given that I saw one of your previous questions before it was deleted, two things. #1) `forEach` is not a `for loop`. So if your instructor explicitly wanted you to use a for loop, your answer is going to fail. #2) A forEach is not going to stop processing once it finds what it is looking for. So if you set it to the true case for element 3, and element 4 doesn't match, your if logic is going to mark it as not found, even though it was previously found. – Taplar Sep 25 '20 at 18:50
  • 1
    the proper syntax for "forEach" is `arr.forEach(callback(currentValue [, index [, array]])[, thisArg])` still i dont understand droid their – Radiant Ahmed Sep 25 '20 at 18:51

7 Answers7

1

you are using forEach wrong . forEach function is to loop through an array. you should use find to do this . something like this

function droids(arr) {
    let result = '';
        if ( arr.find(item => item === "Droids") ){
            result = "Found Droid!"
        } else {
            result = "Droid Not Found"
        }  
    return result;
  }
Mohammad Faisal
  • 2,265
  • 1
  • 10
  • 16
1

You may want to use a different function arr.includes

Like so

function findItem(arr, value) {
  let result = '';
  if (arr.includes(value)) {
    result = `Found ${value}!`
  } else {
    result = `${value} Not Found`
  }
  return result;
}

const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"]
const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]

// Pass in the array into `findItem` and then the value
console.log(findItem(starWars, "Droids"))
console.log(findItem(thrones, "Droids"))
Eduards
  • 1,734
  • 2
  • 12
  • 37
1

Since you have to use the forEach() method, you could use this function that returns true if it finds the string "Droids". I would, personally, use a conventional for loop in order to stop the loop once the string has been found. If you want to stop the forEach once the string is found, you will have to throw an exception.

function findDroids(array) {
    let flag = false;
    array.forEach(element => {
        if (element == "droids") flag = true; 
    });
    return flag;
}
Victor Santizo
  • 1,145
  • 2
  • 7
  • 16
0

forEach is wrong here. Below is the simplified answer. You can use indexOf instead of find

function droids(arr) { 
        return  arr.indexOf("Droids") !== -1 ? "Found Droid!" :"Droid Not Found";
      }
Sarun UK
  • 6,210
  • 7
  • 23
  • 48
0

if your are not forced to use foreeach her is a simple answer

const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"] 
const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]
function droids(arr) {
  let result = '';
  for(i=0;i<arr.length;i++){
  if (arr[i]==="Droids")
        {
        result = "Found Droid!";
        } else
        {result = "Droid Not Found"}  

 }
    return result;

}

console.log(droids(starWars)) 
console.log(droids(thrones))
redred1
  • 1
  • 2
0

You can use indexOf or ES6 feature some function as below.

array.IndexOf:

function droids(arr) { 
 return arr.indexOf("Droids") !== -1 ? "Found Droid!" :"Droid Not Found"; 
}

array.some:

    function droids(arr) { 
        return arr.some(x => x === "Droids") ? "Found Droid!" :"Droid Not Found"; 
   }

The some function executes the callback function once for each element in the array until it finds the one where the callback function returns a true. The some() method immediately returns true and doesn’t evaluate the remaining elements.

Ayaz
  • 2,111
  • 4
  • 13
  • 16
0

function findWord(arr, word) {
  return arr.indexOf(word) !== -1 ? true : false;
}

const array = ["Luke", "Finn", "Rey", "Kylo", "Droids"] 

var res = findWord(array, 'Droids')
console.log(res)
Abdul Moiz
  • 447
  • 2
  • 10