前端笔试题记录
写一个find方法,根据id查找name;
const data = [
{
id: "1000", name: "二哈",
children: {}
},
{
id: "1001", name: "柯基",
children: {
id: "1010", name: "柴犬",
children: {
id: "1100", name: "边牧",
children: {
id: "1101", name: "比特",
}
}
}
},
{
id: "1002", name: "比熊",
children: {
id: "1020", name: "希高地"
}
},
]
function find(data, id) {
for (let i in data) {
if (data[i].id == id) {
console.log((data[i].name))
} else if (data[i].children) {
find(data[i].children, id);
}
}
}
find(data, "1100")
//bug: find(data, "1100")无法查找到,无法返回未找到
判断版本号大小;
let v1 = "10.2.3.1"
let v2 = "10.3.3.1"
function judgeVersion(v1, v2) {
if (v1 === v2) { return 0 }
const arr1 = v1.split(".").map(i => parseInt(i))
const arr2 = v2.split(".").map(i => parseInt(i))
for (let i = 0; i < arr1.length; i++) {
return arr1[i]>arr2[i]?1:-1
}
}
console.log(judgeVersion(v1, v2))