返回
手撕JS高手进阶(持续更新中…)
前端
2023-11-24 00:09:23
关于实现js中一些常见的方法属于面试中的常问问题,可能刚开始接触的时候会一筹莫展。知道和理解其中的原理能够在日常开发中更如鱼得水,面对面试也不成问题。
另外,学会以目的(实现的功能)为导向一层一层反推,总结出实现的思路就能按照步骤直接实现或者曲线实现(整理不易记得点赞哈)。
1. 实现一个函数,返回一个数组中最大值和最小值
function maxAndMin(array) {
if (array.length === 0) {
return { max: null, min: null };
}
let max = array[0];
let min = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
return { max, min };
}
const array1 = [1, 2, 3, 4, 5];
const result1 = maxAndMin(array1);
console.log(result1); // { max: 5, min: 1 }
const array2 = [10, 5, 20, 15, 30];
const result2 = maxAndMin(array2);
console.log(result2); // { max: 30, min: 5 }
2. 实现一个函数,判断一个字符串是否是回文
function isPalindrome(string) {
if (string.length === 0) {
return true;
}
string = string.toLowerCase().replace(/[^a-zA-Z0-9]/g, "");
const reversedString = string.split("").reverse().join("");
return string === reversedString;
}
const string1 = "racecar";
const result1 = isPalindrome(string1);
console.log(result1); // true
const string2 = "hello world";
const result2 = isPalindrome(string2);
console.log(result2); // false
3. 实现一个函数,计算两个日期之间的天数
function daysBetween(date1, date2) {
const oneDay = 1000 * 60 * 60 * 24;
const difference = Math.abs(date1 - date2);
return Math.ceil(difference / oneDay);
}
const date1 = new Date("2023-03-08");
const date2 = new Date("2023-03-15");
const result1 = daysBetween(date1, date2);
console.log(result1); // 7
const date3 = new Date("2023-03-08");
const date4 = new Date("2023-04-08");
const result2 = daysBetween(date3, date4);
console.log(result2); // 31
4. 实现一个函数,打乱一个数组
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
const array1 = [1, 2, 3, 4, 5];
const result1 = shuffleArray(array1);
console.log(result1); // [4, 1, 5, 2, 3]
const array2 = [10, 5, 20, 15, 30];
const result2 = shuffleArray(array2);
console.log(result2); // [30, 10, 5, 15, 20]
5. 实现一个函数,将一个整数转换为罗马数字
function intToRoman(num) {
const romanNumerals = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M",
};
let result = "";
for (const value in romanNumerals) {
while (num >= value) {
result += romanNumerals[value];
num -= value;
}
}
return result;
}
const num1 = 3;
const result1 = intToRoman(num1);
console.log(result1); // "III"
const num2 = 4;
const result2 = intToRoman(num2);
console.log(result2); // "IV"
const num3 = 9;
const result3 = intToRoman(num3);
console.log(result3); // "IX"
const num4 = 49;
const result4 = intToRoman(num4);
console.log(result4); // "XLIX"
const num5 = 1990;
const result5 = intToRoman(num5);
console.log(result5); // "MCMXC"
6. 实现一个函数,将一个罗马数字转换为整数
function romanToInt(roman) {
const romanNumerals = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
let result = 0;
let previousValue = 0;
for (let i = roman.length - 1; i >= 0; i--) {
const currentValue = romanNumerals[roman[i]];
if (currentValue < previousValue) {
result -= currentValue;
} else {
result += currentValue;
}
previousValue = currentValue;
}
return result;
}
const roman1 = "III";
const result1 = romanToInt(roman1);
console.log(result1); // 3
const roman2 = "IV";
const result2 = romanToInt(roman2);
console.log(result2); // 4
const roman3 = "IX";
const result3 = romanToInt(roman3);
console.log(result3); // 9
const roman4 = "XLIX";
const result4 = romanToInt(roman4);
console.log(result4); // 49
const roman5 = "MCMXC";
const result5 = romanToInt(roman5);
console.log(result5); // 1990
7. 实现一个函数,判断一个数组是否包含重复元素
function containsDuplicates(array) {
const set = new Set();
for (let i = 0; i < array.length; i++) {
if (set.has(array[i])) {
return true;
} else {
set.add(array[i]);
}
}
return false;
}
const array1 = [1, 2, 3, 4, 5];
const result1 = containsDuplicates(array1);
console.log(result1); // false
const array2 = [1, 2, 3, 4, 1];
const result2 = containsDuplicates(array2);
console.log(result2); // true
const array3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1];
const result3 = containsDuplicates(array3);
console.log(result3); // true