gpt4 book ai didi

javascript - 在javascript中将数字10.0反转为0.01

转载 作者:行者123 更新时间:2023-11-28 12:15:10 24 4
gpt4 key购买 nike

我正在尝试实现一个在 JavaScript 中反转给定数字(int/float)的函数。函数实现为

function reverseNumber(n) {
let reversed = 0;
const sign = Math.sign(n)
n *= sign;

const exponent = n.toString().indexOf('.');
if (exponent !== -1) n *= Math.pow(10, n.toString().length - 1 - exponent);
while (n !== 0) {
reversed *= 10;
reversed += n % 10;
n = Math.floor(n / 10);
}
if (exponent !== -1) reversed /= Math.pow(10, exponent);
return reversed * sign;
}

该函数通过了以下测试用例

  expect(reverseNumber(0)).toEqual(0);
expect(reverseNumber(15)).toEqual(51);
expect(reverseNumber(90)).toEqual(9);
expect(reverseNumber(-5)).toEqual(-5);
expect(reverseNumber(-90)).toEqual(-9);
expect(reverseNumber(-2359)).toEqual(-9532);
expect(reverseNumber(12.403)).toEqual(304.21);
expect(reverseNumber(-12.4)).toEqual(-4.21);

唯一失败的情况是输入为 10.0 时。测试用例是

expect(reverseNumber(10.0)).toEqual(0.01);

我没有找到任何方法来反转 10.0 => 0.01。你们能帮忙吗?谢谢

最佳答案

既然您似乎很乐意在字符串级别工作,最简单的事情可能就是转换为字符串,将其转换为数组,反转数组,然后将它们全部放回一起 - 同时允许使用减号:

function reverseNumber(num) {
// Remember if it's negative
var isNegative = num < 0;
// Get the digits and decimal just for the absolute part
var chars = (isNegative ? -num : num).toString().split("");
// Reverse them
chars.reverse();
// Put it back together and convert back to a number
var num = Number(chars.join(""));
// Return the result, converting back to negative if appropriate
return isNegative ? -num : num;
}

对于教学来说,这很冗长,当然,其中许多操作可以链接在一起。

您的测试示例:

function reverseNumber(num) {
// Remember if it's negative
var isNegative = num < 0;
// Get the digits and decimal just for the absolute part
var chars = (isNegative ? -num : num).toString().split("");
// Reverse them
chars.reverse();
// Put it back together and convert back to a number
var num = Number(chars.join(""));
// Return the result, converting back to negative if appropriate
return isNegative ? -num : num;
}

function test(num, expect) {
var result = reverseNumber(num);
console.log(num, " => ", result, result == expect ? "OK" : "ERROR");
}

test(0, 0);
test(15, 51);
test(90, 9);
test(-5, -5);
test(-90, -9);
test(-2359, -9532);
test(12.403, 304.21);
test(-12.4, -4.21);
.as-console-wrapper {
max-height: 100% !important;
}

<小时/>

重新编辑:

The only case its failing is when input is 10.0 . test case is

expect(reverseNumber(10.0)).toEqual(0.01);

I am not finding any way to reverse 10.0 => 0.01.

如果您的起点是一个数字,那么您就不能这样做,否则会弄乱其他几个测试用例。作为数字,1010.0 之间没有区别,因此如果 10.0(即 10)=> 0.01,然后 15(即 15.0)=> 0.51)。但你说过它应该是 51

如果您的起点是字符串,您就可以做到这一点,但是:

function reverseNumber(numStr) {
// Remember if it's negative
var isNegative = numStr[0] === "-";
// Get the digits and decimal just for the absolute part
var chars = (isNegative ? numStr.substring(1) : numStr).split("");
// Reverse them
chars.reverse();
// Put it back together and convert back to a number
var num = Number(chars.join(""));
// Return the result, converting back to negative if appropriate
return String(isNegative ? -num : num);
}

function test(num, expect) {
var result = reverseNumber(num);
console.log(num, " => ", result, result == expect ? "OK" : "ERROR");
}

test("0", "0");
test("15", "51");
test("90", "9");
test("-5", "-5");
test("-90", "-9");
test("-2359", "-9532");
test("12.403", "304.21");
test("-12.4", "-4.21");
test("10.0", "0.01");
.as-console-wrapper {
max-height: 100% !important;
}

关于javascript - 在javascript中将数字10.0反转为0.01,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com