2

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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
pers0n
  • 135
  • 8
  • 1
    3.22 acres isn't the same thing as 3 acres and 22 karats, if there are 24 karats in an acre it would be 3 acres and just over 5 karats – jonrsharpe Jan 31 '20 at 08:25
  • the way you calculated this was (22 / 100) * 24 which would give us exactly 5.28 karats, right? @jonrsharpe – pers0n Jan 31 '20 at 08:29
  • @jonrsharpe look this whole thing I'm trying to do here is my uncle asked me about a program that could calculate this thing for him because he couldn't do it in Microsoft Excel so I told him why don't I make you a program, and what I understood from him is that he wants it specifically the way I showed it up in the question, he also told me that that's the way they calculate it in his work, so idk. – pers0n Jan 31 '20 at 08:42

2 Answers2

1

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));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hey @Nina, first of all thanks a lot for replying, secondly and unfortunately that didn't quite work for me cause it's dynamic on all numbers it's only working on the example I gave in the question, but I appreciate your effort, now can you make the same thing but dynamic on all numbers cause I tried a couple of things I tried changing the 42 to 24 in the second code snippet you wrote and it kinda works on some numbers like 3.15 and 2.15 it was 6.06 which correct but when I changed it again to 3.22 and 2.2 it was 6.18, so that's not the way it should be. I'll continue in another comment – pers0n Jan 31 '20 at 12:43
  • I also tried some things and added some bits of code `function sumD(a, b) { function getValue(v) { return +(v.toString().match(/\.(\d*)/)[1] || '').padEnd(2, 0); } var i = Math.floor(a + b), p = getValue(a) + getValue(b); var test = Math.fround((a + b) - i); var x = getValue(test); var strr = toString(x).length -2; var y = parseInt(('1'.toString()).padEnd(strr, '0')); var z = Math.floor(x / y); if (p > 23) { p -= z; ++i; } return i + p / 100; } console.log(sumD(3.22, 2.2));` and that worked on some numbers, so idk. sry the code is cluttered. – pers0n Jan 31 '20 at 12:45
  • should `4.2` be `2` or `20` for the places? – Nina Scholz Jan 31 '20 at 12:48
  • If you mean the float after the decimal point, then yeah it should be 2 not 20. – pers0n Jan 31 '20 at 12:55
  • because 3 acres and 22 karats + 2 acres and 2 karats = 6 acres not 6 acres and 16 karats, or at least that's how my uncle told me to make it – pers0n Jan 31 '20 at 13:03
  • cause my uncle is the one who wants this thing cause he couldn't do a formula for it in Microsoft Excel so I told him I'll make a program for you. just though I should get this out of the way – pers0n Jan 31 '20 at 13:07
  • i deleted the padding and changed the default empty string. please see edit. – Nina Scholz Jan 31 '20 at 13:11
  • I don't know how you did that, but it's working now, thank you so much for your effort – pers0n Jan 31 '20 at 13:17
0

Separate the decimal values from your numbers.(Already done) Ex: 3.22 -> 0.22 and 2.2 -> 0.2

Add them -> 0.22 + 0.2

Divide them by 0.24 -> (0.22+0.2)/.24 = 1

Add that to the wtotal -> 3.00 + 2.00 = 5 -> 5 + 1

I think this should be the logic mathematically.

0o'-Varun-'o0
  • 735
  • 1
  • 5
  • 22
  • Hey @0o'-Varun-'o0 Thanks for replying. That's what I've trying to do but the code I think the problem is with the code I wrote, but the logic I get. – pers0n Jan 31 '20 at 12:58