Tell us what’s happening:
The code is failing the automated validation of the decimal button action.
Your code so far
const numberContainsDecimal = (number) => {
return number.toString().indexOf('.') !== -1 ? true : false;
}
const lastCharIsDecimal = (number) => {
return number.charAt(calcInput.length-1) !== '.' ? true : false
}
const handleDecimal = () => {
if (!(numberContainsDecimal(calcInput) && lastCharIsDecimal(calcInput))) {
setCalcInput(Number(calcInput) + '.');
}
}
const handleNumber = (number) => {
number = Number(number);
if (number === 0 && numberContainsDecimal(calcInput)) {
// Use string to include trailing zeros if the number contains a decimal
setCalcInput(calcInput + '' + number)
} else {
// Otherwise convert to number to remove leading zeros
setCalcInput(Number(calcInput + '' + number));
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36
Challenge: Build a JavaScript Calculator
Link to the challenge: