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.
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));
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
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.should
4.2
be2
or20
for the places?If you mean the float after the decimal point, then yeah it should be 2 not 20.
why is
6.18
wrong?