Original link: leetcode-cn.com/problems/fi…

Answer:

  1. The ASCII code for each character is a fixed number.
  2. The characters in s and T are identical except for one, which is the ASCII code.
  3. Simply sum the ASCII numbers of all characters in T and subtract the sum of the ASCII numbers of characters in S, leaving the ASCII numbers of characters added to T.
  4. useString.fromCharCodeConvert the ASCII code to a string.
  5. willcharCodeAtandString.fromCharCodereplacecodePointAtandString.fromCodePointSame effect.
/ * * *@param {string} s
 * @param {string} t
 * @return {character}* /
var findTheDifference = function (s, t) {
  let code = 0; // Save the ASCII value

  // sum the ASCII values of all characters in t
  for (const char of t) {
    code += char.charCodeAt(0);
  }

  // Since t has one more character than s, simply subtract the ASCII of s from the ASCII of t, and the remaining character is the added character
  for (const char of s) {
    code -= char.charCodeAt(0);
  }

  // Convert the ASCII code to a string and get the result
  return String.fromCharCode(code);
};
Copy the code