Paldinromes
Directions
주어진 문자열이 회문(회기식)이면 true 를 반환하고, 그렇지 않으면 false 를 반환하기.
Examples
palindrome('abba') === true
palindrome('abcdefg') === false
1.
가장 기본적인 방법으로는 자바스크립트 내장함수인 'reverse'를 사용하여 구현할 수 있습니다.
function palindrome(str) {
const reversed = str
.split('')
.reverse()
.join('')
return str === reversed
}
module.exports = palindrome
2.
만약 'reverse' 매소드를 사용할 수 없는 경우라면, every 매소드를 사용하여 구현할 수 있습니다.
function palindrome(str) {
return str.split('').every((char, i) => {
return char === str[str.length - i - 1]
})
}
module.exports = palindrome
Test
TIP
아래와 같은 방법으로 함수를 실행시키면 쉽게 디버깅을 할 수 있습니다.
palindrome('abba') // true
palindrome('abcdefg') // false
const palindrome = require('./index')
test('palindrome function is defined', () => {
expect(typeof palindrome).toEqual('function')
})
test('"aba" is a palindrome', () => {
expect(palindrome('aba')).toBeTruthy()
})
test('" aba" is not a palindrome', () => {
expect(palindrome(' aba')).toBeFalsy()
})
test('"aba " is not a palindrome', () => {
expect(palindrome('aba ')).toBeFalsy()
})
test('"greetings" is not a palindrome', () => {
expect(palindrome('greetings')).toBeFalsy()
})
test('"1000000001" a palindrome', () => {
expect(palindrome('1000000001')).toBeTruthy()
})
test('"Fish hsif" is not a palindrome', () => {
expect(palindrome('Fish hsif')).toBeFalsy()
})
test('"pennep" a palindrome', () => {
expect(palindrome('pennep')).toBeTruthy()
})