String compare

Write a function to compare sets of two sentences, each consisting of words, and returns unique

I might have come up with a better code using some build in functions in Javascript, but here is my code:

//Function to write a string into an array, and eliminate the . , ! : ; and ?. It also writes the words to lower case.

const stringToArray = (string) => {

  // Use split to write a string into and array of words.
  let array = string.split(' ');

  // Now check each word for . , ! and ?
  for (let i = 0; i < array.length; i++) {

    let word = array[i];

    word = word.split('.')[0];
    word = word.split(',')[0];
    word = word.split('!')[0];
    word = word.split('?')[0];
    word = word.split(';')[0];
    word = word.split(':')[0];

    array[i] = word.toLowerCase();
  }

  console.log(array);

  return array;
  
}

//Now lets write a function to compare two strings and write a new string composed of unique words.

const getDifferences = (string1, string2) => {

  // Write the strings as arrays without lettercases and punctuations.

  array1 = stringToArray(string1);
  array2 = stringToArray(string2);

  // The array to be converted into a string at the end of the function

  let doubleIndex1 = [];
  let doubleIndex2 = [];

  // Compare arrays and push the index of double words to a new array.

  for (let i = 0; i < array1.length; i++) {
    for (let y = 0; y < array2.length; y++) {
      if (array1[i] == array2[y]) {
        doubleIndex1.push(i);
        doubleIndex2.push(y);
      }
    }
  }
  
  // Reverse the indexes as to remove doubles from end to start

  doubleIndex1.reverse();
  doubleIndex2.reverse();
  
  
  //Now remove the doubles from each array.
  
  // Array1
  for (const index of doubleIndex1) {
    array1.splice(index, 1);
  }
  
  //Array2
  for (const index of doubleIndex2) {
    array2.splice(index, 1);
  }

  // Combine the arrays into 1.  
  
  let joinedArray = array1.concat(array2);
  
  //Return a string.
  
  return joinedArray.join(' ');
}

// Two strings to test it with:

const str1 = 'Hello the world of wonders.';
const str2 = 'This is the world!';

// Log the result of calling getDifferences on the two strings

console.log(getDifferences(str1, str2)); // Expected output: 'hello of wonders this is'

Found a shorter function online, but it will require my stringToArray function to eliminate Capitals and punctuation.

let array1 = stringToArray(str1);
let array2 = stringToArray(str2);

let unique1 = array1.filter((o) => array2.indexOf(o) === -1);
let unique2 = array2.filter((o) => array1.indexOf(o) === -1);

const unique = unique1.concat(unique2);

console.log(unique.join(' '));

I just need to get my head round how it works :thinking:.