-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakeBinary.js
36 lines (33 loc) · 875 Bytes
/
fakeBinary.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
// my solutions
// P - a str of num
// R - str of num where each num < 5 will be = 0 else 1
// E - '17823' -> '01100'
// P - split the str into arr
// loop throw arr for each
// math.abs each el and compare values
// += to str each outcome from the forEach
function fakeBin(x) {
let xs = x.split('')
let resultingStr = '';
xs.forEach(el => {
Math.abs(el) >= 5 ? resultingStr += 1 : resultingStr += 0
})
return resultingStr
}
// my other
function fakeBin(x) {
let xs = x.split('')
let resultingStr = []
xs.forEach(el => {
Math.abs(el) >= 5 ? resultingStr.push(1) : resultingStr.push(0)
})
return resultingStr.join('')
}
// codeWars
function fakeBin(str) {
var newStr = "";
for (var i = 0; i < str.length; i++) {
Number(str[i]) >= 5 ? newStr += 1 : newStr += 0;
}
return newStr;
}