思路:判断传入的两个字符串的长度,如果长度不一致就返回false,长度一样的话,就遍历这两个字符串,结合使用charCodeAt方法。charCodeAt该方法是 返回的是Unicode 编码值。利用Unicode 编码值之和来判断两个字符串的是否一致。如果它们Unicode 编码值之和相同说明字符串是相同的,那就返回true,反之就返回false。
const isSame = (str1, str2) => {
let CodeAt1 = 0;
let CodeAt2 = 0;
if (str1.length === str2.length) {
for (let i = 0; i < str1.length; i++) {
CodeAt1 += (str1.charCodeAt(i));
}
for (let j = 0; j < str2.length; j++) {
CodeAt2 += str2.charCodeAt(j);
}
if (CodeAt1 === CodeAt2) {
return true;
} else {
return false;
}
} else {
return false;
}
}
console.log(isSame('abcdd', 'accb'));