我看到了这样一段代码。我查了 MDN,但没有提到 toString 有参数。 3 在 n.toString(3) 中做什么?
function solution(n) {
n = n.toString(3).split('').reverse().join('')
return parseInt(n, 3)
}
最佳答案
这是 Number.prototype.toString()
,而不是 Array.prototype.toString()
。它采用 radix
作为可选参数。
An integer in the range 2 through 36 specifying the base to use for representing numeric values.
您的代码将 n
转换为 base-3。如果你想将数字转换为二进制,你可以执行 n.toString(2)
const n = 16,
bases = [2, 3, 4, 10, 16]
console.log(
bases.map(radix => n.toString(radix))
)
关于javascript - Array toString 中的参数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67433140/