首页 > web前端 > js教程 > 正文

JavaScript 面试备忘单 - 第 1 部分

DDD
发布: 2024-12-11 20:42:02
转载
305人浏览过

javascript 面试备忘单 - 第 1 部分

数组运算

// 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;
}
登录后复制

以上就是JavaScript 面试备忘单 - 第 1 部分的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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