Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript string1이 string2로 끝나는지 여부 확인

<시간/>

string1과 string2라는 두 개의 문자열을 받아서 string1이 string2로 끝나는지 여부를 결정하는 JavaScript 함수를 작성해야 합니다.

예를 들어 -

"The game is on"
Here, "on" should return true

동안

"the game is off"
Above, “of" should return false

이 함수에 대한 코드를 작성해 봅시다 -

예시

const first = 'The game is on';
const second = ' on';
const endsWith = (first, second) => {
   const { length } = second;
   const { length: l } = first;
   const sub = first.substr(l - length, length);
   return sub === second;
};
console.log(endsWith(first, second));
console.log(endsWith(first, ' off'));

출력

콘솔의 출력은 -

true
false