I have written a function to search an array and log a number(x) if it is found. The code below works as expected and 1 is logged.
let myArr = [54, 23, 499, 342, 1, 44];
let x = 1;
let searchNumber = (arr) => {
arr.forEach(ele => {
if (ele == x) {
console.log(ele);
}
})
};
searchNumber(myArr);
I'd like to now return the number that I assign to variable x. Expecting y to be assigned 499 but when I log y it returns undefined. Where am I going wrong?
let myArr = [54, 23, 499, 342, 1, 44];
let x = 499;
let searchNumber = (arr) => {
arr.forEach(ele => {
if (ele == x) {
return ele;
}
})
};
let y = searchNumber(myArr);
I have written a function to search an array and log a number(x) if it is found. The code below works as expected and 1 is logged.
let myArr = [54, 23, 499, 342, 1, 44];
let x = 1;
let searchNumber = (arr) => {
arr.forEach(ele => {
if (ele == x) {
console.log(ele);
}
})
};
searchNumber(myArr);
I'd like to now return the number that I assign to variable x. Expecting y to be assigned 499 but when I log y it returns undefined. Where am I going wrong?
let myArr = [54, 23, 499, 342, 1, 44];
let x = 499;
let searchNumber = (arr) => {
arr.forEach(ele => {
if (ele == x) {
return ele;
}
})
};
let y = searchNumber(myArr);
Share
Improve this question
edited Oct 29, 2020 at 16:38
Derek Wang
10.2k4 gold badges19 silver badges40 bronze badges
asked Oct 29, 2020 at 14:22
btew2102btew2102
716 bronze badges
1
- you can use Array.prototype.find to return the first element that satisfies a condition. – Lucas D Commented Oct 31, 2020 at 5:51
5 Answers
Reset to default 6return ele
inside forEach
callback is not the return
of the searchNumber
function.
forEach
executes a provided function once for each array element so return ele
inside that will act like return
to that provided function inside forEach
.
It does not repesent the return
to the main function.
In this case, it's better to use for
loop.
let myArr = [54, 23, 499, 342, 1, 44];
let x = 499;
let searchNumber = (arr) => {
for (let i = 0; i < arr.length; i ++) {
if (arr[i] == x) {
return arr[i];
}
}
};
let y = searchNumber(myArr);
console.log(y);
If you look at the placement of the return statement, it is actually within the arrow function being called for each element (ele => {...}
). No value is actually being returned from the scope of the function searchNumber
.
Try creating a variable in the scope of searchNumber
and modifying it from arr.forEach()
instead:
let myArr = [54, 23, 499, 342, 1, 44];
let x = 499;
let searchNumber = (arr) => {
let val = null;
arr.forEach(ele => {
if (ele == x) {
val = ele;
}
});
return val;
};
let y = searchNumber(myArr);
You cannot break (exit) a forEach, it will iterate all elements. the array.find() should do the trick https://www.w3schools./jsref/jsref_find.asp or array.findIndex() https://www.w3schools./jsref/jsref_findindex.asp
.find
will return the matching element:
let myArr = [54, 23, 499, 342, 1, 44];
let x = 499;
let searchNumber = (arr) => arr.find(ele => ele == x);
let y = searchNumber(myArr);
console.log(y);
what you are looking here is probably the find function.
let searchNumber = (arr) => arr.find(e => e === x);
let y = searchNumber(myArr);