最新笔试题

💻 前端最新笔试题🏠专栏:前端面试题
👀个人主页:繁星学编程🍁
🧑个人简介:一个不断提高自我的平凡人🚀
🔊分享方向:目前主攻前端,其他知识也会阶段性分享🍀
👊格言:☀️没有走不通的路,只有不敢走的人!☀️
👉让我们一起进步,一起成为更好的自己!!!🎁

最新笔试题

一. 优化elseif代码

请用两种以上方式优化下面方法代码。

Function getClothes(temperature){
    let clothes = "";
    if (temperature === 5) {
      clothes = "羽绒服";
    } else if (temperature === 10) {
      clothes = "鸭绒服";
    }else if (temperature === 15) {
      clothes = "毛呢外套";
    }else if (temperature === 20) {
      clothes = "西服";
    }else if (temperature === 25) {
      clothes = "长袖";
    }else if (temperature === 30) {
      clothes = "短袖";
    }
    return clothes;
}

方式一:使用对象键值对形式

function getClothes(temperature) {
  const clothesMap = {
    5: "羽绒服",
    10: "鸭绒服",
    15: "毛呢外套",
    20: "西服",
    25: "长袖",
    30: "短袖"
  };
  return clothesMap[temperature] || "";
}

方式二:使用数组对象,然后通过find()方法查找的方式

function getClothes(temperature) {
  const clothesArray = [
    { temperature: 5, clothes: "羽绒服" },
    { temperature: 10, clothes: "鸭绒服" },
    { temperature: 15, clothes: "毛呢外套" },
    { temperature: 20, clothes: "西服" },
    { temperature: 25, clothes: "长袖" },
    { temperature: 30, clothes: "短袖" }
  ];
  const matchedClothes = clothesArray.find((item) => item.temperature === temperature);
  return matchedClothes ? matchedClothes.clothes : "";
}

二. 数字转货币格式

正则表达式

const number = 1234567;
const formattedNumber = number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(formattedNumber);

直接使用tolocaleString()方法

const number = 1234567;
const formattedNumber = number.toLocaleString();
console.log(formattedNumber);

三. 给定一个字符串数组lines,每一个元素代表一个IP地址,找到出现频率最高的IP。

注:给定数据只有一个频率最高的IP

样例:

输入 = ["192.168.1.1","192.118.2.1","192.168.1.1"]
输出 "192.168.1.1"
function findMostFrequent(lines) {
let counts = {};

// 统计每个IP出现次数
for (let ip of lines) {
  if (!counts[ip]) {
    counts[ip] = 0;
  }
  counts[ip]++;
}

// 找到出现次数最多的
let mostFrequent = "";
let maxCount = 0;

for (let ip in counts) {
  if (counts[ip] > maxCount) {
    mostFrequent = ip;
    maxCount = counts[ip];
  }
}

return mostFrequent;
}

四. 寻找n个数中的最大值。

注:保证列表里的所有数字都在int范围内

样例:

输入:[1,2,3,4,5]
输出:5
function findMax(arr) {
return Math.max(...arr);
}

五. 反转一个只有3位数的偶数。

注:你可以假设输入一定是一个只有三位数的整数,这个整数大于等于100,小于1000.

样例:

输入:number = 123
输出:321

function reverseNumber(num) {
if (num >= 100 && num < 1000) {
  // 将数字转换为字符串,然后将其拆分为字符数组,再反转数组,最后使用join方法将字符数组合并为一个字符串
  return parseInt(String(num).split('').reverse().join(''));
} else {
  // 输入不是一个三位数的整数,返回原始输入
  return num;
}
}

六. 给出一个字符c,如果它是一个数字或字母,返回true,否则返回false。

样例:

输入:c = ‘1’
输出:true
解释:'1’属于数字

function isAlphaNumeric(number) {
return /^[a-zA-Z0-9]$/.test(number);
}

结束语

希望对您有一点点帮助,如有错误欢迎小伙伴指正。
👍点赞:您的赞赏是我前进的动力!
⭐收藏:您的支持我是创作的源泉!
✍评论:您的建议是我改进的良药!
一起加油!!!💪💪💪

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值