-2

I have a code that simply compares str2 (User input) with str1 (our reference string ) to check if any word in str2 is typed wrongly or correctly. The code works fine but I can't find a solution to this problem:

I want to ignore extra spaces, commas and dots and any other writing signs to be compared in both strings. just like a dictation app...

for example, below strings should assume as equal strings on output:

str1 = "I was sent to earth"

str2 = "I was, sent to:       earth."

Any other modifications to improve this code is extremely appreciated. Please Help...

var str1 = "I was sent to earth to protect my cousin";
var str2 = "I waz Sent to earth to protet my cousine";

let a = str1.toLowerCase().split(' ');
let b = str2.toLowerCase().split(' ');
let res1 = b.filter(i => !a.includes(i));
let res2 = a.filter(i => !b.includes(i));
console.log(res1);
console.log(res2);

var str1 = res1.toString();
str1 = str1.replace(/,/g, '\n');

var str2 = res2.toString();
str2 = str2.replace(/,/g, '\n');

var player = GetPlayer();
player.SetVar("wrong_Spell",str1);
player.SetVar("right_Spell",str2);
Sara Ree
  • 3,417
  • 12
  • 48

1 Answers1

1

You could use match to extract the words using \w+:

function words(s) {
    return s.toLowerCase().match(/\w+/g);
}

var str1 = "I was sent to earth to protect my cousin";
var str2 = "I waz Sent to earth to protet my cousine";

let a = words(str1);
let b = words(str2);
let res1 = b.filter(i => !a.includes(i));
let res2 = a.filter(i => !b.includes(i));
console.log(res1);
console.log(res2);
trincot
  • 317,000
  • 35
  • 244
  • 286