DEV Community

Kevin Kim
Kevin Kim

Posted on

JS Solutions for CodeWars Katas: Part 2

1.Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.

moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]

My solution:

const moveZeros = arr => arr.filter(num => num !== 0).concat(arr.filter(num => num === 0))
Enter fullscreen mode Exit fullscreen mode

Long version:

function moveZeros(arr) {
  let zerosArr = arr.filter(num => num === 0)
  let nonZerosArr = arr.filter(num => num !== 0)
  return nonZerosArr.concat(zerosArr)
}
Enter fullscreen mode Exit fullscreen mode

2.Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"
The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parentheses!

const createPhoneNumber = numbers =>
   `(${numbers.slice(0,3).join('')}) ${numbers.slice(3,6).join('')}-${numbers.slice(6, 10).join('')}`
Enter fullscreen mode Exit fullscreen mode

Top comments (0)