문장 문자열과 문자를 받는 JavaScript 함수를 작성해야 하며 이 함수는 해당 특정 문자로 시작하는 문자열의 모든 단어를 반대로 해야 합니다.
예:문자열이 -
인 경우const str = 'hello world, how are you';
특정 문자 'h'로 시작하기 -
그런 다음 출력 문자열은 -
여야 합니다.const output = 'olleh world, woh are you';
즉, "h"로 시작하는 단어, 즉 Hello와 How를 반대로 했습니다.
예시
다음은 코드입니다 -
const str = 'hello world, how are you';
const reverseStartingWith = (str, char) => {
const strArr = str.split(' ');
return strArr.reduce((acc, val) => {
if(val[0] !== char){
acc.push(val);
return acc;
};
acc.push(val.split('').reverse().join(''));
return acc;
}, []).join(' ');
};
console.log(reverseStartingWith(str, 'h')); 출력
다음은 콘솔의 출력입니다 -
olleh world, woh are you