simpleCalc/calc.js

91 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2024-01-12 08:03:49 -05:00
//----init----
//define shit
let total = 0;
let display = 0;
let last = 'init';
let store = 0;
let crumbs = [];
//on numIn
//write string to display
function updateDisplay(wit){
document.getElementById('display').innerText = parseFloat(wit);
}
function numIn(x) {
display = '' + display + x;
updateDisplay(display);
}
function del(){
display = 0;
updateDisplay(total);
}
function makeCrumbs(bread){
let crumb = 'nope';
switch (bread) {
case 'div':
crumb = '/';
break;
case 'mul':
crumb = '*';
break;
case 'sub':
crumb = '-';
break;
case 'add':
crumb = '+';
break;
case 'tot':
crumb = '=';
break;
case 'exp':
crumb = '^';
}
crumbs.push(parseFloat(display));
crumbs.push(crumb);
document.getElementById('breadcrumbs').innerText = crumbs.join(' ');
}
//on oprIn
function calcOp(opr) {
//convert display string to float
let input = parseFloat(display);
//update breadcrumbs
makeCrumbs(opr);
//copy display to store
store = parseFloat(display);
//if init copy display to total and update last
if (last == 'init'){
total = parseFloat(display);
}
//do math
switch(last){
case 'div':
total = total / parseInt(display);
break;
case 'mul':
total = total * parseInt(display);
break;
case 'sub':
total = total - parseInt(display);
break;
case 'add':
total = total + parseInt(display);
break;
case 'exp':
total = total ** parseInt(display);
break;
}
//clear display and show total, set last
del();
last = opr;
//logs
console.log(store);
console.log(total);
console.log(last);
console.log(typeof store);
console.log(typeof total);
}