https://programmers.co.kr/learn/courses/30/lessons/12922
체감 난이도:☆☆ (String메서드 공부하기)
function solution(num) {
const arr = Array(num).fill('_');
const watermelon = arr.map((el, idx) => (idx % 2 ? '박' : '수'));
return watermelon.join('');
}
console.log(solution(3)); // "수박수"
console.log(solution(4)); // "수박수박"
function solution(num) {
return '수박'.repeat(num / 2) + (num % 2 ? '수' : '');
}
function solution(num) {
// return '수박'.repeat(num).substring(0, num);
return '수박'.repeat(num).slice(0, num);
}
'abc'.repeat(-1); // RangeError ← 음수는 안됨
'abc'.repeat(0); // ''
'abc'.repeat(1); // 'abc'
'abc'.repeat(2); // 'abcabc'
'abc'.repeat(3.5); // 'abcabcabc' ← 정수로 바뀜(3)
'abc'.repeat(1/0); // RangeError ← 무한은 안됨
const anyString = 'Mozilla';
// Displays 'M'
console.log(anyString.substring(0, 1)); // M
console.log(anyString.substring(1, 0)); // M (0,1)과 같음
// Displays 'Mozill'
console.log(anyString.substring(0, 6)); // Mozill
// Displays 'lla'
console.log(anyString.substring(4)); // lla
console.log(anyString.substring(4, 7)); // lla
console.log(anyString.substring(7, 4)); // lla (4,7)과 같음
// Displays 'Mozilla'
console.log(anyString.substring(0, 7)); // Mozilla
console.log(anyString.substring(0, 10)); // Mozilla