我想按照教程反转数字 t。首先我传递了一个数字作为参数。我已经使用 to String 函数将数字转换为字符串。然后我使用 split("").reverse().join("") 来反转数字。 我的代码如下:
<script type="text/javascript">
function reverse_the_string(num) {
num = String(num);
// str = num.toString;
num.split("").reverse().join("");
return;
}
console.log(reverse_the_string(56789));
</script>
但是我得到一个未定义的错误?谁能解释一下哪里出了问题?
最佳答案
您不返回结果。使用
return num.split("").reverse().join("");
如果你只使用 return , 然后返回 undefined
。
function reverse_the_string(num) {
return String(num).split("").reverse().join("");
}
document.write(reverse_the_string(56789));
注意:反转仅适用于小数字!
关于javascript - 在javascript中反转数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687201/