How to remove the letters from word1 if they exist in word2 with JavaScript

1 Answer

0 votes
function removeCommonLetters(word1, word2) {
  let result = '';
  
  for (let char of word1) {
    if (!word2.includes(char)) {
      result += char;
    }
  }
  
  return result;
}

const word1 = "forest";
const word2 = "tor";

const result = removeCommonLetters(word1, word2);

console.log(result); 



/*
run:

fes

*/

 



answered Jul 9, 2025 by avibootz
...