在javascript中反转数字10.0到0.01

RED*_*SAD 0 javascript logic

我试图在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;
}
Run Code Online (Sandbox Code Playgroud)

此函数通过以下测试用例

  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);
Run Code Online (Sandbox Code Playgroud)

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

expect(reverseNumber(10.0)).toEqual(0.01);
Run Code Online (Sandbox Code Playgroud)

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

T.J*_*der 6

既然你似乎很乐意在字符串级别工作,最简单的事情可能是转换为字符串,将其转换为数组,反转数组,并将它们全部重新组合在一起 - 同时允许减号:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

测试示例:

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);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
  max-height: 100% !important;
}
Run Code Online (Sandbox Code Playgroud)


重新编辑:

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

expect(reverseNumber(10.0)).toEqual(0.01);
Run Code Online (Sandbox Code Playgroud)

我没有找到任何方法来反转10.0 => 0.01.

如果您的起点是一个数字,那么你就不能搞清楚其他几个测试用例.作为一个数字,10和之间没有区别10.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");
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
  max-height: 100% !important;
}
Run Code Online (Sandbox Code Playgroud)