JavaScript string charCodeAt() vs codePointAt() All In One
JavaScript string charCodeAt() vs codePointAt() All In One
String.prototype.charCodeAt()vsString.prototype.codePointAt()
String 值的 charCodeAt() 方法返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元。
取值范围: [0, 2^16]
2 ** 16
// 65536
2 ** 16 - 1
// 65535
'h'.charCodeAt()
//104
'h'.charCodeAt(0)
//104
'h'.charCodeAt(1)
// NaN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
String 值的 codePointAt() 方法返回一个非负整数,它是从给定索引开始的字符的 Unicode 代码点值。
'h'.codePointAt()
// 104
'h'.codePointAt(0)
// 'h'.codePointAt()
'h'.codePointAt(1)
// undefined
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt
solutions
function scoreOfString(s: string): number {
let sum: number = 0;
for(let i = 0; i < s.length - 1; i++) {
sum += Math.abs(s.charCodeAt(i) - s.charCodeAt(i+1));
}
return sum;
};
// function scoreOfString(s: string): number {
// let sum: number = 0;
// for(let i = 0; i < s.length - 1; i++) {
// sum += Math.abs(s[i].charCodeAt(0) - s[i+1].charCodeAt(0));
// // sum += arr[i] = Math.abs(s[i].codePointAt(0) - s[i + 1].codePointAt(0));
// }
// return sum;
// };
demos
function scoreOfString(s: string): number {
let arr: number[] = new Array(s.length - 1).fill(0);
for(let i = 0; i < s.length - 1; i++) {
arr[i] = Math.abs(s[i].charCodeAt(0) - s[i+1].charCodeAt(0));
// arr[i] = Math.abs(s[i].codePointAt(0) - s[i + 1].codePointAt(0));
}
return arr.reduce((sum, i) => sum += i, 0);
};
/*
Line 5: Char 20: error TS2554: Expected 1 arguments, but got 0.
'h'.charCodeAt()
104
'h'.charCodeAt(0)
104
'h'.charCodeAt(1)
NaN
'h'.codePointAt()
104
'h'.codePointAt(0)
104
'h'.codePointAt(1)
undefined
*/
/*
'h'.codePointAt()
104
'h'.charCodeAt()
104
*/

https://leetcode.com/problems/score-of-a-string/?envType=daily-question&envId=2024-06-01
function scoreOfString(s: string): number {
const dict = {};
// const begin = `a`.charCodeAt(0);
// const end = `z`.charCodeAt(0);
// for (let i = begin; i <= end; i++) {
for (let i = 97; i <= 122; i++) {
dict[String.fromCodePoint(i)] = i;
// dict[String.fromCharCode(i)] = i;
}
// console.log(`dict =`, dict);
let arr: number[] = new Array(s.length - 1).fill(0);
for(let i = 0; i < s.length - 1; i++) {
arr[i] = Math.abs(dict[s[i]] - dict[s[i + 1]]);
}
return arr.reduce((sum, i) => sum += i, 0);
};
/*
const begin = `A`.charCodeAt();
const end = `z`.charCodeAt();
let dict = {};
for (let i = begin; i <= end; i++) {
dict[String.fromCodePoint(i)] = i;
}
console.log(`dict =`, dict);
// {A: 65, ..., y: 121}
const begin = `A`.charCodeAt();
const end = `z`.charCodeAt();
let dict = {};
for (let i = begin; i <= end; i++) {
dict[i] = String.fromCodePoint(i)
}
console.log(`dict =`, dict);
// {65: 'A', ..., 121: y}
*/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
String.fromCodePoint
convert
numberorstringtoASCII
String.fromCodePoint(65);
//"A"
String.fromCodePoint(`65`);
//"A"

String.fromCharCode
String.fromCharCode(65)
// "A"
String.fromCharCode(`65`);
// "A"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
refs
convert number or string to ASCII in JavaScript All In One

https://www.cnblogs.com/xgqfrms/p/13333814.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18234188
未经授权禁止转载,违者必究!

浙公网安备 33010602011771号