why this code doesn't return the right output for number greater than 9 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why this code doesn't return the right output for number greater than 9 ?

const arr = [10, 11, 7, 5, 100, 6, 4, 3, 8, 9]; function num(e) { for (let e of arr) { if (e.toString().split('').includes('10')) { return 'Yay num found'; } } return 'Num not found'; } console.log(num(arr)); // the output will be Num not found // and the output supposed to be yay num found and if i check num less than 10 I get the correct output!

23rd Dec 2021, 3:57 PM
Wael Kamal
Wael Kamal - avatar
1 Answer
+ 3
Let us take the 1st element which is 10 e.toString().split('') returns ['1','0'] which doesn't have a '10'. So it will never become true. Hope you get the point.
23rd Dec 2021, 4:29 PM
Avinesh
Avinesh - avatar