Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function asteriscIt(n) {
- // convert parameter to a string
- // remove all commas and split into an array
- let arr = n.toString().replace(/,/g, '').split('');
- let result = [];
- for ( let i = 0; i < arr.length; i++) {
- if ( arr[i] % 2 === 0 && arr[i + 1] % 2 === 0) {
- // if arr[i] is even && arr[i + 1] is even
- result.push(arr[i] + '*')
- // result = arr[i] *
- } else {
- result.push(arr[i])
- //result = arr[i]
- }
- }
- return result.join('')
- // convert array back to string
- };
- asteriscIt(5312708)
- 5312708 ==> "531270*8"
- asteriscIt([1, 2, 4, 8, 16])
- [1, 2, 4, 8, 16] ==> "12*4*816"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement