将布尔值转换为数字。有更好的算法吗?

huangapple go评论57阅读模式
英文:

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 }]

LIVE HERE

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 === &quot;boolean&quot;) return +value;
if (typeof value === &quot;object&quot;) for (const k in value) value[k] = booleanToInt(value[k]);
return value;
}
console.log(booleanToInt(&quot;qwerty&quot;)); // &#39;qwerty&#39;
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1
console.log(booleanToInt([1, &quot;qwerty&quot;, false])); // [1, &#39;qwerty&#39;, 0]
console.log(booleanToInt([1, &quot;qwerty&quot;, { a: true }])); //[1, &#39;qwerty&#39;, { a: 1 }]

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 03:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定