Home > OS >  I have made a simple calculator but How I can prevent users from entering dot several times in one n
I have made a simple calculator but How I can prevent users from entering dot several times in one n

Time:03-06

if

(previousNum == "." && currentNum == ".") { screen.value = screen.value.substring(0, numchar - 1);

CodePudding user response:

You are using javascript. you can split it on "." then count the length of the array which should be 2 or less.

var x = '12.34.56';
var arr = x.split(".")
var number_of_dots = arr.length - 1
console.log(number_of_dots)

CodePudding user response:

I think you can check if the number is Integer or float with this sample function

function isFloat(n){
    return Number(n) === n && n % 1 !== 0;
}
function isInt(n) {
   return n % 1 === 0;
}

and write catch, so if had error the entered number is not valid for you calculator

  • Related