Finding mistakes in a string - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this sting. We can assume that the two strings we are getting as argument will always have the same length.

We have to return the number of mistakes that exist in the first array.

Example

Following is the code −

const str1 = 'Tkos am er exakgrg fetwnh';
const str2 = 'This is an example string';
const countMistakes = (mistaken, correct) => {
let count = 0;
for(let i = 0; i < mistaken.length; i++){
if(mistaken[i] === correct[i]){
continue;
};
count++;
};
return count;
};
console.log(countMistakes(str1, str2));

Output

Following is the output in the console −

15
Published on 18-Sep-2020 09:21:38