I'm trying to write a function that checks whether a number exists in given array or not?The number I've decided tomcheck for is "7" and I want to get it even if it's as a part of a number. e.g, 97... I'm a newbie to javascript so, would really appreciate if someone could explain the solution in details... I've also attached my sample of code as well.
function sevenBoom(arr) {
let singleElement;
let string;
let includeSeven;
for (let i=0; i<arr.length; i++){
singleElement = arr[i];
string = singleElement.toString();
includeSeven = string.includes("7");
}
if (includeSeven == true){
return "Boom!"
}else{
return "There is no 7 in the array";
}
}
arr = [1,2,4,5,6,7,8,9];
let result = sevenBoom(arr);
console.log(result);
You can simply use arr.some(a => a.toString().includes(7));
some() will be efficient more efficient as it will stop at the first matching condition and won't go further to check rest of the values in the array. Use .toString() to convert number to string and then we can use includes to check if values present in that string or not. Further details regarding to methods are provided below.
Array.prototype.some() : The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
String.prototype.includes() : The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
Array.prototype.includes() : The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
Try it below.
let arr = [1,2,4,5,6,7,8,9];
let result = arr.some(a => a.toString().includes(7));
console.log(result);
let arr2 = [1,97];
let result2 = arr2.some(a => a.toString().includes(7));
console.log(result2);
let arr3 = [1,6];
let result3 = arr3.some(a => a.toString().includes(7));
console.log(result3)
There are multiple solutions that would work. I am discussing an approach which is quite simple to understand.
function sevenBoom(arr) {
// Initial array: [1, 2, 4, 5, 6, 7, 8, 9]
// "map" will convert the number array to string array
// Array after map: ["1", "2", "4", "5", "6", "7", "8", "9"]
// "join" will join all the string numbers
// Joined string: "12456789"
// "includes" will check if "7" is in the joined string
const hasSeven = arr.map(n => `${n}`).join("").includes("7");
return hasSeven ? "Boom!" : "There is no 7 in the array";
};
let result = sevenBoom([1,2,4,5,6,7,8,9]);
console.log(result);
result = sevenBoom([1,97]);
console.log(result);
result = sevenBoom([1,2]);
console.log(result);
The issue with your code is that, you have to exit the for loop execution, when you find an occurance of seven, else it will iterate the complete array and your variable includeSeven will holds only the last comparison result.
Here your loop will run against each element in the array arr. When the loop executes the element 7 in the array, it will set includeSeven as true. But the loop executes further and eveluates the rest of elements 8 and 9. So includeSeven will be holding the result of last execution. So you should exit the loop when you have got a match.
Working Fiddle
function sevenBoom(arr) {
let singleElement;
let string;
let includeSeven;
for (let i = 0; i < arr.length; i++) {
singleElement = arr[i];
string = singleElement.toString();
includeSeven = string.includes("7");
if (includeSeven) {
i = arr.length; // Implementation of break
}
}
if (includeSeven == true) {
return "Boom!"
} else {
return "There is no 7 in the array";
}
}
arr = [1, 2, 4, 5, 6, 7, 8, 9];
console.log(sevenBoom(arr));
OR
Simple One line function that checks there is any element in the array having 7 as a digit somewhere in the elements using Array.some and String.includes.
Here Array.some will check if at least one element in the array matches the condition that we specify. String.includes whether the orr string includes a specific element. So we check any one the element in array has a string 7
Working Fiddle
sevenBoom = (arr) => arr.some((item) => item.toString().includes("7")) ? "Boom!" : "There is no 7 in the array";
arr = [1, 2, 4, 5, 6, 7, 8, 9];
console.log(sevenBoom(arr));