Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. function asteriscIt(n) {
  2. // convert parameter to a string
  3. // remove all commas and split into an array
  4. let arr = n.toString().replace(/,/g, '').split('');
  5. let result = [];
  6. for ( let i = 0; i < arr.length; i++) {
  7. if ( arr[i] % 2 === 0 && arr[i + 1] % 2 === 0) {
  8. // if arr[i] is even && arr[i + 1] is even
  9. result.push(arr[i] + '*')
  10. // result = arr[i] *
  11. } else {
  12. result.push(arr[i])
  13. //result = arr[i]
  14. }
  15. }
  16. return result.join('')
  17. // convert array back to string
  18. };
  19.  
  20. asteriscIt(5312708)
  21. 5312708 ==> "531270*8"
  22. asteriscIt([1, 2, 4, 8, 16])
  23. [1, 2, 4, 8, 16] ==> "12*4*816"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement