0

How do i use the toFixed() method only if the strings are more than 12? Otherwise i would like it to display normally, so it is like a normal calculator app. I have used the max character method but it doesn't look nice and it shouldn't be used.

enter image description here

$(document).ready(function(){

var inputs=[""];

var totalString;

var operators1=["+", "-", "/", "*"];
//opertors array with the . for validation
var operators2=["."];

var nums = [0,1,2,3,4,5,6,7,8,9];

function getValue(input){
if(operators2.includes(inputs[inputs.length-1])===true && input==="."){
  console.log("Duplicate '.'");
}
else if(inputs.length===1 && operators1.includes(input)===false)
        {
          inputs.push(input);
        }
else if(operators1.includes(inputs[inputs.length-1])===false){
  inputs.push(input);
}
else if(nums.includes(Number(input))){
  inputs.push(input);
}
update();
}
function update(){
totalString = inputs.join("");
$("#steps").html(totalString);
console.log(inputs);
}
function getTotal(){
totalString = inputs.join("");
$("#steps").html(eval(totalString));

}
$("a").on("click", function(){
if(this.id==="deleteAll"){
  inputs=[""];
  update();
}
else if(this.id==="backOne"){
  inputs.pop();
  update();

}
else if(this.id==="total"){
  getTotal();

}

else{

  if(inputs[inputs.length-1].indexOf("+","-","/","*")===-1)
  {
    getValue(this.id);
  }
    else
    {
      getValue(this.id);
    }
}
});

});
prasanth
  • 22,145
  • 4
  • 29
  • 53
eyedfox
  • 714
  • 1
  • 7
  • 14
  • 1
    You could even bother not using toFixed, eg.. `(1234567890.123).toString().substring(0,12)` You might just need to put an extra check for when there is a '.' only at the end, but you should get the idea. – Keith Nov 16 '16 at 11:12
  • @Keith Thanks a lot man. It worked! – eyedfox Nov 16 '16 at 11:53

1 Answers1

0

if you really have to use toFixed then it could be like so:

let string = "333453453453453453";
string.length > 12 && (+string).toFixed();
Alex Bykov
  • 718
  • 6
  • 13