Why this code gives undefined 🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why this code gives undefined 🤔

Please anyone help me in this code. Why this give undefined?? https://code.sololearn.com/WwIhNM9Y82sR/?ref=app

2nd Nov 2020, 12:31 PM
Akash Agrawal
Akash Agrawal - avatar
5 Answers
+ 5
Akash Agrawal the "return true" code is returning the function provided as parameter for arr.forEach not the show function. If you want to return true try this: for (let i of arr) if (i==5) return true; See more about array. forEach here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
2nd Nov 2020, 12:53 PM
VEDANG
VEDANG - avatar
+ 5
Because your function is not returning anything.
2nd Nov 2020, 12:46 PM
VEDANG
VEDANG - avatar
+ 3
Thanks VEDANG
2nd Nov 2020, 12:55 PM
Akash Agrawal
Akash Agrawal - avatar
+ 2
VEDANG but why my function is not return true
2nd Nov 2020, 12:51 PM
Akash Agrawal
Akash Agrawal - avatar
+ 2
A forEach loop cannot be broken before completion. You can use a traditional for loop as VEDANG suggested. Or use recursion https://code.sololearn.com/WymH0U5NRiBf
2nd Nov 2020, 1:11 PM
Ore
Ore - avatar