英文:
Convert bool to number. Better Algorithm?
问题
最近,我在一家公司面试,并收到了一个测试任务:
实现一个接受任何数据类型并将布尔类型(如果有的话)转换为数值的函数。可以使用任何嵌套级别的对象、数组、字符串、数字等作为参数。
示例:
booleanToInt('qwerty') // 'qwerty'
booleanToInt(1) // 1
booleanToInt(false) // 0
booleanToInt(true) // 1
booleanToInt([1, 'qwerty', false]) // [1, 'qwerty', 0]
booleanToInt([1, 'qwerty', { a: true }]) // [1, 'qwerty', { a: 1 }]
booleanToInt({ a: { b: true }, c: false, d: 'qwerty' }) // { a: { b: 1 }, c: 0, d: 'qwerty' }
我写了自己的实现,但被告知这太复杂并且是一个庞大的解决方案。在我询问如何优化答案的问题上,并未给出答案。
我的决定:
// 检查对象
function isObject(value) {
return typeof value === "object" && !Array.isArray(value) && value !== null;
}
// 转换对象
function transformObject(obj) {
Object.keys(obj).forEach((key) => {
if (isObject(obj[key])) {
transformObject(obj[key]);
} else if (Array.isArray(obj[key])) {
transformArray(obj[key]);
} else {
obj[key] = transformStatic(obj[key]);
}
});
}
// 转换数组
function transformArray(list) {
list.forEach((item, i) => {
if (isObject(item)) {
transformObject(item);
} else if (Array.isArray(item)) {
transformArray(item);
} else {
list[i] = transformStatic(item);
}
});
}
// 转换静态类型
function transformStatic(value) {
let res = value;
if (typeof value === "boolean") {
res = +value;
}
return res;
}
// 主函数
function booleanToInt(value) {
if (isObject(value)) {
transformObject(value);
return value;
}
if (Array.isArray(value)) {
transformArray(value);
return value;
}
return transformStatic(value);
}
console.log(booleanToInt('qwerty')); // 'qwerty'
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1
console.log(booleanToInt([1, 'qwerty', false])); // [1, 'qwerty', 0]
console.log(booleanToInt([1, 'qwerty', { a: true }])); //[1, 'qwerty', { a: 1 }]
我想知道还能想出什么其他算法吗?也许会更短更简洁?
英文:
Recently, I interviewed in one company and received a test task:
Implement a function that accepts any data type and convert the boolean type (if any) to a numeric value.
Objects of any nesting level, arrays, strings, numbers, etc. can be used as parameters.
Example:
booleanToInt('qwerty') // 'qwerty'
booleanToInt(1) // 1
booleanToInt(false) // 0
booleanToInt(true) // 1
booleanToInt([1, 'qwerty', false]) // [1, 'qwerty', 0]
booleanToInt([1, 'qwerty', { a: true }]) // [1, 'qwerty', { a: 1 }]
booleanToInt({ a: { b: true }, c: false, d: 'qwerty' }) // { a: { b: 1 }, c: 0, d: 'qwerty' }
I wrote my own implementation, but I was told that this is too complicated and a big solution. To my question, what and where can I optimize the answer was not given to me.
My decision:
// Checking for an object
function isObject(value) {
return typeof value === "object" && !Array.isArray(value) && value !== null;
}
// Transforming an object
function transformObject(obj) {
Object.keys(obj).forEach((key) => {
if (isObject(obj[key])) {
transformObject(obj[key]);
} else if (Array.isArray(obj[key])) {
transformArray(obj[key]);
} else {
obj[key] = transformStatic(obj[key]);
}
});
}
// Transforming the array
function transformArray(list) {
list.forEach((item, i) => {
if (isObject(item)) {
transformObject(item);
} else if (Array.isArray(item)) {
transformArray(item);
} else {
list[i] = transformStatic(item);
}
});
}
// Transforming Static Types
function transformStatic(value) {
let res = value;
if (typeof value === "boolean") {
res = +value;
}
return res;
}
// main function
function booleanToInt(value) {
if (isObject(value)) {
transformObject(value);
return value;
}
if (Array.isArray(value)) {
transformArray(value);
return value;
}
return transformStatic(value);
}
console.log(booleanToInt("qwerty")); // 'qwerty'
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1
console.log(booleanToInt([1, "qwerty", false])); // [1, 'qwerty', 0]
console.log(booleanToInt([1, "qwerty", { a: true }])); //[1, 'qwerty', { a: 1 }]
I wonder what other algorithm can come up with?
Perhaps it will be shorter and more concise?
答案1
得分: 4
Sure, here is the translated code portion:
function booleanToInt(value) {
if (typeof value === "boolean") return +value;
if (typeof value === "object") for (const k in value) value[k] = booleanToInt(value[k]);
return value;
}
console.log(booleanToInt("qwerty")); // 'qwerty'
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1
console.log(booleanToInt([1, "qwerty", false])); // [1, 'qwerty', 0]
console.log(booleanToInt([1, "qwerty", { a: true })); //[1, 'qwerty', { a: 1 }]
I have translated the code and removed the unnecessary parts as per your request.
英文:
I'd simplify it to single function and use for ... in
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function booleanToInt(value) {
if (typeof value === "boolean") return +value;
if (typeof value === "object") for (const k in value) value[k] = booleanToInt(value[k]);
return value;
}
console.log(booleanToInt("qwerty")); // 'qwerty'
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1
console.log(booleanToInt([1, "qwerty", false])); // [1, 'qwerty', 0]
console.log(booleanToInt([1, "qwerty", { a: true }])); //[1, 'qwerty', { a: 1 }]
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论