mumbling.js (7kyu 05)

Codewars 알고리즘 풀이


Description

  • The examples below show you how to write function ‘mumbling’.
    • 아래 예제가 함수 ‘mumbling’을 어떻게 작성하는지 보여준다.


For example:

mumbling(‘abcd’); // A-Bb-Ccc-Dddd

mumbling(‘code’); // C-Oo-Ddd-Eeee



Solution 01

function mumbling(str) {
  let result = [];
  
  for (let i = 0; i < str.length; i++) {
    let temporary = '';
    
    for (let j = 0; j < i + 1; j++) {
      temporary += j === 0 ? str[i].toUpperCase() : str[i].toLowerCase();
    }
    
    result.push(temporary);
  }
  
  return result.join('-');
}

mumbling('abcd');  // A-Bb-Ccc-Dddd
mumbling('code');  // C-Oo-Ddd-Eeee


Solution 02

function mumbling(str) {
  let result = [];
  
  for (let i = 0; i < str.length; i++) {
    result.push(str.charAt(i).toUpperCase() + str.charAt(i).toLowerCase().repeat(i));
  }
  
  return result.join('-');
}

mumbling('abcd');  // A-Bb-Ccc-Dddd
mumbling('code');  // C-Oo-Ddd-Eeee


Solution 03

function mumbling(str) {
  return str.split('').map((i, index) => i.toUpperCase() + i.toLowerCase().repeat(index)).join('-');
}

mumbling('abcd');  // A-Bb-Ccc-Dddd
mumbling('code');  // C-Oo-Ddd-Eeee


Solution 04

function mumbling(str) {
  return [...str].map((i, index) => {
    return i.toUpperCase() + i.toLowerCase().repeat(index);
  }).join('-');
}

mumbling('abcd');  // A-Bb-Ccc-Dddd
mumbling('code');  // C-Oo-Ddd-Eeee