Code Wars: Kebabize (a lot of edge cases)

https://www.codewars.com/kata/57f8ff867a28db569e000c4a/train/javascript

Modify the kebabize function so that it converts a camel case string into a kebab case.
kebabize('camelsHaveThreeHumps') // camels-have-three-humps
kebabize('camelsHave3Humps') // camels-have-humps
Notes:
  • the returned string should only contain lowercase letters


function kebabize(str) {
  let arr = str.split("");
 
  for (let i = arr.length - 1; i >= 0; i--) {
    if (Number(arr[i]) || arr[i] === "0") {
      arr.splice(i, 1);
      console.log('number')
    }
    else if (arr[i] === arr[i].toUpperCase() && !Number.isInteger(arr[i]) && i !== 0) {
      arr.splice(i, 0, "-");
    }
  }
  if (arr[0] === "-") {
    arr.splice(0, 1);
  }
  return arr.join("").toLowerCase();
}

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int