0

0

JavaScript 面试备忘单 - 第 1 部分

DDD

DDD

发布时间:2024-12-11 20:42:02

|

353人浏览过

|

来源于dev.to

转载

javascript 面试备忘单 - 第 1 部分

论论App
论论App

AI文献搜索、学术讨论平台,涵盖了各类学术期刊、学位、会议论文,助力科研。

下载

数组运算

// initialize
const arr = [];
const arr = new array(size).fill(0);  // [0,0,0,0,0]
const arr = array.from({length: n}, (_, i) => i);  // [0,1,2,...,n-1]

// basic operations
arr.push(element);     // add to end
arr.pop();            // remove from end
arr.unshift(element); // add to start
arr.shift();          // remove from start

// slicing and splicing
arr.slice(startidx, endidx);  // returns new array, endidx not included
arr.splice(startidx, deletecount, ...itemstoadd);

// common methods
arr.map(x => x * 2);           // returns new array
arr.filter(x => x > 0);        // returns new array
arr.reduce((acc, curr) => acc + curr, initialvalue);
arr.sort((a, b) => a - b);     // ascending
arr.reverse();
arr.join('');                  // convert to string
arr.includes(element);         // check existence
arr.indexof(element);          // first occurrence
arr.lastindexof(element);      // last occurrence

字符串操作

// creation and access
const str = "hello";
str.length;
str[0] or str.charat(0);

// common methods
str.substring(startidx, endidx);   // endidx not included
str.substr(startidx, length);      // deprecated but good to know
str.slice(startidx, endidx);       // can use negative indices
str.split('');                     // convert to array
str.tolowercase();
str.touppercase();
str.trim();                        // remove whitespace
str.replace(old, new);
str.replaceall(old, new);
str.startswith(prefix);
str.endswith(suffix);
str.includes(substr);
str.repeat(count);

地图和设置

// map
const map = new map();
map.set(key, value);
map.get(key);
map.has(key);
map.delete(key);
map.clear();
map.size;

// set
const set = new set();
set.add(value);
set.has(value);
set.delete(value);
set.clear();
set.size;

// object as hashmap
const obj = {};
obj[key] = value;
key in obj;                    // check existence
delete obj[key];
object.keys(obj);
object.values(obj);
object.entries(obj);

类和对象

class node {
    constructor(val) {
        this.val = val;
        this.next = null;
    }
}

// quick object creation
const obj = { key1: value1, key2: value2 };

通用数据结构

// queue using array
const queue = [];
queue.push(element);    // enqueue
queue.shift();         // dequeue

// stack using array
const stack = [];
stack.push(element);
stack.pop();

// linkedlist node
class listnode {
    constructor(val = 0, next = null) {
        this.val = val;
        this.next = next;
    }
}

// binary tree node
class treenode {
    constructor(val = 0, left = null, right = null) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

// trie node
class trienode {
    constructor() {
        this.children = new map();
        this.isendofword = false;
    }
}

位操作

// common operations
n << 1;               // multiply by 2
n >> 1;               // divide by 2
n & 1;                // check if odd
n & (n-1);            // remove last set bit
n & -n;               // get last set bit
n | (1 << pos);       // set bit at position
n & ~(1 << pos);      // clear bit at position
n ^ (1 << pos);       // toggle bit at position

常见模式和实用程序

// number operations
math.max(...arr);
math.min(...arr);
math.floor(n);
math.ceil(n);
math.abs(n);
number.max_safe_integer;
number.min_safe_integer;
infinity;
-infinity;

// random number
math.random();                     // [0, 1)
math.floor(math.random() * n);     // [0, n-1]

// character code
'a'.charcodeat(0);                 // 97
string.fromcharcode(97);           // 'a'

// check type
number.isinteger(n);
array.isarray(arr);
typeof variable;

// parsing
parseint(str);
parsefloat(str);

常见的面试模式

// Two Pointers
let left = 0, right = arr.length - 1;
while (left < right) {
    // process
    left++;
    right--;
}

// Sliding Window
let left = 0;
for (let right = 0; right < arr.length; right++) {
    // add arr[right] to window
    while (/* window condition */) {
        // remove arr[left] from window
        left++;
    }
}

// Binary Search
let left = 0, right = arr.length - 1;
while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
}

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

340

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

212

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1503

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

625

2023.11.24

java读取文件转成字符串的方法
java读取文件转成字符串的方法

Java8引入了新的文件I/O API,使用java.nio.file.Files类读取文件内容更加方便。对于较旧版本的Java,可以使用java.io.FileReader和java.io.BufferedReader来读取文件。在这些方法中,你需要将文件路径替换为你的实际文件路径,并且可能需要处理可能的IOException异常。想了解更多java的相关内容,可以阅读本专题下面的文章。

655

2024.03.22

php中定义字符串的方式
php中定义字符串的方式

php中定义字符串的方式:单引号;双引号;heredoc语法等等。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

610

2024.04.29

go语言字符串相关教程
go语言字符串相关教程

本专题整合了go语言字符串相关教程,阅读专题下面的文章了解更多详细内容。

173

2025.07.29

c++字符串相关教程
c++字符串相关教程

本专题整合了c++字符串相关教程,阅读专题下面的文章了解更多详细内容。

83

2025.08.07

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

54

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
SQL 教程
SQL 教程

共61课时 | 3.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号