-
Notifications
You must be signed in to change notification settings - Fork 0
/
seven_boom_edabit.js
69 lines (52 loc) · 1.98 KB
/
seven_boom_edabit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Create a function that takes an array of numbers and return "Boom!" if the digit 7 appears in the array. Otherwise, return "there is no 7 in the array".
Examples
sevenBoom([1, 2, 3, 4, 5, 6, 7]) ➞ "Boom!"
// 7 contains the number seven.
sevenBoom([8, 6, 33, 100]) ➞ "there is no 7 in the array"
// None of the items contain 7 within them.
sevenBoom([2, 55, 60, 97, 86]) ➞ "Boom!"
// 97 contains the number seven.
PACER
Problem
Create a function sevenBoom() that returns "Boom!" if 7 is present in any element of the array - including any digit of the element - or return "there is no 7 in the array" otherwise.
Input: array of numbers
Output: String
Restrictions: none
Algorithm:
Define a function sevenBoom() that takes an array of numbers arrNum as a param
declare a variable str and initialize it to an empty string
declare a variable reges and initialize it to a new regular expression object "7"
iterate over every element od arrNum an transfor each element to an string
add each str element to string
call the test() RegExp method passing the string as a param
return "Boom!" if string contains "7" otherwise return "there is no 7 in the array"
*/
/*
function sevenBoom(arrNum) {
let str = "";
let regex = new RegExp("7", "g");
for(let i = 0; i < arrNum.length; i += 1) {
str += new String(arrNum[i]);
}
return regex.test(str) ? "Boom!" : "there is no 7 in the array";
}
*/
// Refactor
function sevenBoom(arrNum) {
return new RegExp("7", "g").test(arrNum.map(num => new String(num))) ?
"Boom!" :
"there is no 7 in the array";
}
console.log(sevenBoom([1, 2, 3, 4, 5, 6, 7])); // "Boom!"
console.log(sevenBoom([8, 100, 33, 6])); // "there is no 7 in the array"
console.log(sevenBoom([2, 55, 60, 97, 86])); // "Boom!"
/*
ADDITIONAL SOLUTIONS:
function sevenBoom(arr) {
return arr.join("").includes("7") ? "Boom!" : "there is no 7 in the array";
}
const sevenBoom =(arr) =>{
return arr.toString().includes("7") ? "Boom!":"there is no 7 in the array"
}
*/