Function Iterations Inquiry

Bryan McCormack

I have been given the task of creating a function that iterates through an array. Below is what I have, but I’m getting undefined. What am I missing?

function lookingForDave(arr) {
  for (var i = 0; i < arr; i++) {
    if (array.forEach === 'Dave') {
      return 'I found him';
    } else {
      return 'Not Dave';
    }

  }
}
var testArray = ['Dave'];

console.log(lookingForDave(testArray));

Peter Halligan

You could use find rather than a for loop. It looks as if you want to return after you first find a match. Array.find will return the value of the first matching criteria.

In the below case if the element in the array is equal to Dave. If there is no match then we will iterate through each item in the array and return undefined.

We can then use the conditional (ternary) operator to test if an item was found.

function lookingForDave (arr){
   const found = arr.find(el => el === 'Dave');
   return found ? 'found him' : 'Not Dave'; 
} 
let testArray = ['Dave'];
lookingForDave(testArray);

conditional (ternary) operator

Array find

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Recursion: Unsure of the callback function's value during the iterations

分類Dev

Session/Cookies Expiration inquiry

分類Dev

Query Criteria Inquiry

分類Dev

Inquiry about a method of class Scanner

分類Dev

Nested Array loop iterations

分類Dev

For loop skipping iterations

分類Dev

Program to generate iterations

分類Dev

Program to generate iterations

分類Dev

Iterations with multiple releases in TFS

分類Dev

Simple Python inquiry - MD5 hashing

分類Dev

Inquiry about try-with-resources statement

分類Dev

Sum of all the iterations of a variable in VBA

分類Dev

SelectMany Anonymous Type and Skip Iterations

分類Dev

Inquiry about using if statement with Widget what these dots meaning if(condition) ...[ ]

分類Dev

Visual Studio MVC 5 and SQL Server Express inquiry

分類Dev

Find minimum number of iterations to reach a certain sum

分類Dev

Epochs and Iterations in Deeplearning4j

分類Dev

How to count the number of iterations in a for comprehension in Scala?

分類Dev

Is it possible to limit the number of iterations that Criterion performs?

分類Dev

Is it possible to increment the modulo operator in later loop iterations?

分類Dev

R For Loop Append Dataframe Iterations to Empty Matrix

分類Dev

My BubbleSort class does not count iterations correctly

分類Dev

How to skip multiple iterations in a while loop

分類Dev

10 $digest() iterations reached when using filter

分類Dev

How to Limit Method (3 iterations) by Date Order?

分類Dev

for loop makes pause every 8 million iterations - why?

分類Dev

for loop makes pause every 8 million iterations - why?

分類Dev

java 8 spring spEL repeatable binding slows each iterations

分類Dev

java 8 spring spEL repeatable binding slows each iterations

Related 関連記事

  1. 1

    Recursion: Unsure of the callback function's value during the iterations

  2. 2

    Session/Cookies Expiration inquiry

  3. 3

    Query Criteria Inquiry

  4. 4

    Inquiry about a method of class Scanner

  5. 5

    Nested Array loop iterations

  6. 6

    For loop skipping iterations

  7. 7

    Program to generate iterations

  8. 8

    Program to generate iterations

  9. 9

    Iterations with multiple releases in TFS

  10. 10

    Simple Python inquiry - MD5 hashing

  11. 11

    Inquiry about try-with-resources statement

  12. 12

    Sum of all the iterations of a variable in VBA

  13. 13

    SelectMany Anonymous Type and Skip Iterations

  14. 14

    Inquiry about using if statement with Widget what these dots meaning if(condition) ...[ ]

  15. 15

    Visual Studio MVC 5 and SQL Server Express inquiry

  16. 16

    Find minimum number of iterations to reach a certain sum

  17. 17

    Epochs and Iterations in Deeplearning4j

  18. 18

    How to count the number of iterations in a for comprehension in Scala?

  19. 19

    Is it possible to limit the number of iterations that Criterion performs?

  20. 20

    Is it possible to increment the modulo operator in later loop iterations?

  21. 21

    R For Loop Append Dataframe Iterations to Empty Matrix

  22. 22

    My BubbleSort class does not count iterations correctly

  23. 23

    How to skip multiple iterations in a while loop

  24. 24

    10 $digest() iterations reached when using filter

  25. 25

    How to Limit Method (3 iterations) by Date Order?

  26. 26

    for loop makes pause every 8 million iterations - why?

  27. 27

    for loop makes pause every 8 million iterations - why?

  28. 28

    java 8 spring spEL repeatable binding slows each iterations

  29. 29

    java 8 spring spEL repeatable binding slows each iterations

ホットタグ

アーカイブ