跟大家一起每天进步一丢丢,在学习的路途上不掉队。
[ 经典题 ] 什么是NaN?它的类型是什么?如何可靠地测试一个值是否等于NaN?
console.log(typeof NaN); // ?
console.log(NaN == NaN); // ?
console.log(NaN != NaN); // ?
console.log(NaN === NaN); // ?
console.log(NaN !== NaN); // ?
console.log(Object.isObject(NaN, NaN)); // ?
const isNaN = (value) => {
};
- 试题参考答案跟第二天试题一起更新。
- 希望大家积极参与和讨论。
【前端一题】字符串字节长度、字符数量、指定索引字符,参考答案:
const str = '123你abc好👦👉😄def呀456';
// 获取字符串中字符(码位)数量
const getCharLength = (str) => [...str].length;
console.log(str.length);
console.log(getCharLength(str));
execute result: 👉
// 获取字符串中字节长度
const str = '123你abc好👦👉😄def呀456';
const getByteCount = (str) => {
// 字符串length表示UTF-16码元的数量,一个码元由16位二进制组成,取值范围:[0, 0xffff],占1-2个字节
let count = str.length;
// 索引为码元对应的索引
for(let i = 0, len = count; i < len; i++){
const charCode = str.charCodeAt(i);
if(charCode > 0xff) count++;
}
return count;
};
console.log(getByteCount(str));
execute result: 👉
// 获取指定索引的字符,例如:getCharAt(str, 9)为👉
const str = '123你abc好👦👉😄def呀456';
const getCharAt = (str, index) => [...str].at(index);
for(let i = 0; i < 18; i++){
console.log(getCharAt(str, i));
}
execute result: 👉
专注于原创短更,便于碎片化涉猎知识。希望我走过的路,留下的痕迹,能对你有所启发和帮助。
转发请注明原处,平台投稿请私信。