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

두 문자열에서 차이점 찾기 - JavaScript

<시간/>

두 개의 문자열을 받아서 문자열에서 상응하는 유사점의 수를 찾는 JavaScript 함수를 작성해야 합니다.

대응하는 요소는 동일하지 않은 경우 유사하지 않습니다. 다음이 두 개의 문자열이라고 가정해 보겠습니다. -

const str1 = 'Hello world!!!';
const str2 = 'Hellp world111';

예시

다음은 코드입니다 -

const str1 = 'Hello world!!!';
const str2 = 'Hellp world111';
const dissimilarity = (str1 = '', str2 = '') => {
   let count = 0;
   for(let i = 0; i < str1.length; i++){
      if(str1[i] === str2[i]){
         continue;
      };
      count++;
   };
   return count;
};
console.log(dissimilarity(str1, str2));

출력

다음은 콘솔의 출력입니다 -

4