javascript - 应用程序将数字吐给我,即使它应该返回另一个值

标签 javascript

我正在尝试制作此前端Web应用程序,您可以在其中以这种形式在提示中提供英亩和克拉数,例如3.22并进行计算,并将总数返回给chrome JS控制台

例如,您有3.22英亩的土地,而另一块土地是2.2英亩。如果您得到这些数字的总和,它应该给您5.42,不,我希望它们返回6,因为英亩有24克拉,如果您计算3英亩和22克拉+ 2英亩和2克拉,它应该给您6英亩,那就是我想在这里做些什么。我一直整夜都在尝试,每次在提示符下输入的数字在控制台中吐回来时,这就是我的代码:

window.setTimeout(function() {
var acres = [];
var floats = [];
var wholes = [];
var input = prompt("What would you like to do?");
while (input !== "quit") {
    if (input === "total") {
        console.log("***********");
        acres.forEach(function(total, i) {
            console.log(i + ": " + total);
        })
        console.log("***********");
    } else if (input === "calc") {
        var num = prompt("Please enter a number");
        while (num !== "back") {
            if (num === "back") {
                break;
            }
            acres.push(num);
            var ftotal = 0
            var wtotal = 0;
            floats = [];
            wholes = [];
            for(var i = 0; i < acres.length; i++) {
                alert("entered the for loop");
                var acresNum = acres.pop();
                var str = acresNum.toString();
                var number = Math.floor((str).split(".")[1]);
                floats.push(number);
                ftotal += floats[i];
                //-------------------------
                var num2 = Math.floor(acresNum);
                wholes.push(num2);
                wtotal += wholes[i];
            }
            alert("exited the for loop");
            console.log(ftotal);
            console.log(wtotal);
            if (ftotal > 23) {
                wtotal++;
            }
            acres.push(wtotal + "." + ftotal);
            var num = prompt("Please enter a number");
        }
    }
    var input = prompt("What would you like to do?");
}
console.log("OK, YOU QUIT THE APP");}, 500)

此应用程序的整个逻辑在于else if(input === "calc")区域中的for循环。

This image shows the numbers in the console as I've entered them in the prompt

最佳答案

您可以采用数值方法,但是您陷入了浮点算法(Is floating point math broken?)的陷阱,并得到了一个与给定42值不匹配的数字。

function sum(a, b) {
    var s = a + b,
        i = Math.floor(s),
        p = (s - i) * 100;

    console.log(p);
    if (p >= 42) { // never reached
        p -= 42;
        ++i;
    }
    return i + p / 100;
}

console.log(sum(3.22, 2.2));


作为解决方案,您可以将位置分开为字符串,并添加整数值,然后检查该值是否大于一英亩,然后返回调整后的值。

function sumD(a, b, threshold) {
    return [a, b]
        .map(v => v.toString().split('.'))
        .reduce((r, a) => {
            a.forEach((v, i) => r[i] += +v);
            r[0] += Math.floor(r[1] / threshold);
            r[1] %= threshold;
            return r;
        }, [0, 0])
        .join('.');			
}

console.log(sumD(3.22, 2.2, 24));

关于javascript - 应用程序将数字吐给我,即使它应该返回另一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60000248/

相关文章:

javascript - 解决Graphql的异步函数无法返回通过具有rdf数据的Stardog服务器查询的数据

javascript - 通过串行 AmCharts 中的 `dataLoader` 加载数据不会呈现图表

javascript - 在页面加载之前触发 jQuery 代码,这可能吗?

javascript - 如何用JS让文本显示在复选框后而不需要手动修改HTML?

javascript - 使用 Javascript 移动图片

javascript - 使用 JavaScript 修改页面会删除所有 CSS

javascript - 另一个 div 上的 AngularJS 可折叠/切换类

javascript - JQuery for 循环和数组 - 为什么这个变量未定义?

javascript - 如何通过在 webpack 4 中设置 eslint 来解决导入/未解决问题

javascript - Webpack 加载带有白色边框的 SVG