Warm tip: This article is reproduced from stackoverflow.com, please click
javascript

application spits the numbers back at me even though it should return another value

发布于 2020-03-27 15:41:09

I'm trying to make this front end web application where you provide acres and karats in a prompt in this form e.g. 3.22 and calculates them and give the total back in the chrome JS console

For example, you have 3.22 acres of land and another land that is 2.2 acres. If you get the sum of these numbers it should give you 5.42, no I want them to return 6, because acres have 24 karats and if you calculate 3 acres and 22 karats + 2 acres and 2 karats it should give you 6 acres, that's what I'm trying make here. I've been trying all night and every time the numbers I put in the prompt gets spit back at me in the console, so here's my code:

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)

The whole logic in this application is in that for loop in the else if(input === "calc") area.

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

Questioner
Ali Mohamed
Viewed
16
Nina Scholz 2020-02-01 03:03

You could take a numerical approach, but you went into the trap of floating point arithmetic (Is floating point math broken?) and get a number which does not match the given value of 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));

As solution, you could separate the places as a string and add integer values and check if the value is greater than one acre, then return an adjusted value.

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));