rounding of, limiting or moving comma. JS string in html form. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

rounding of, limiting or moving comma. JS string in html form.

Hi. Put my calculator here. If you get a too big number, it goes wider than the display. I would like to limit the number of decimals in the answer, to make it fit the display. Any suggestions? There is also a check for operator, in the inp() function. I guess its not the prettiest way of doing it. Would love some feedback on that one too. Thanks for looking. https://code.sololearn.com/WJMT3qoIQunw/

25th Nov 2019, 2:09 PM
fataMorgana
fataMorgana - avatar
2 Answers
+ 1
function inp(val=""){ //check to see if last input is an operator // let s = document.getElementById("display").value; (use only "display") let s = display.value; //If val is an operator. Check if last input was an operator if('+-*/'.includes(val) && val != ''){ let r = s.charAt(s.length -1); // I do different, see my code // if(r=='+'||r=='-'||r=='*'||r=='/'){ not use //remove old operator and add new operator // display.value = s.slice(0, -1); // display.value += val; // } // else{ not use display.value += val; // } } else{ display.value += val; } } function solve() { let x = eval(display.value); let y = x.toPrecision(); if(y.includes('.')) y = x.toPrecision(4); display.value = y; } // My similar code https://code.sololearn.com/WV382uzpTslU/#js // Your code is good, create more project and more learn.
25th Nov 2019, 7:22 PM
Jônatas Araripe
Jônatas Araripe - avatar
0
Cool. Thanks👍 Knew ut could be done easier 😉
25th Nov 2019, 7:53 PM
fataMorgana
fataMorgana - avatar