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

두 문자열 JavaScript의 차이점

<시간/>

s와 t라는 두 개의 문자열이 제공됩니다. 문자열 t는 문자열 s를 무작위로 섞어서 생성한 다음 임의의 위치에 하나의 문자를 더 추가합니다.

이 두 문자열을 모두 사용하고 t에 추가된 문자를 반환하는 JavaScript 함수를 작성해야 합니다.

예를 들어 -

입력 문자열이 -

인 경우
const s = "abcd", t = "abcde";

그러면 출력은 다음과 같아야 합니다. -

const output = "e";

''가 추가된 문자이기 때문입니다.

예시

const s = "abcd", t = "abcde";
const findTheDifference = (s, t) => {
   let a = 0, b = 0; let charCode, i = 0;
   while(s[i]){
      a ^= s.charCodeAt(i).toString(2);
      b ^= t.charCodeAt(i).toString(2);
      i++;
   };
   b^=t.charCodeAt(i).toString(2);
   charCode = parseInt(a^b,2);
   return String.fromCharCode(charCode);
};
console.log(findTheDifference(s, t));

출력

콘솔의 출력은 -

e