I’m building a decimal-to-binary number convertor. I need to check if the value returned by the parseInt() function is a number or not. I know that you can use the isNaN() function. This function takes in a string or number as an argument and returns true if it evaluates to NaN. I need the second condition in my if statement to use the isNaN() function to check if the value returned by parseInt() is NaN. I also want to consider only positive numbers to convert, I want to add a third condition in the if statement to check whether the number is less than 0.
This is my code so far
const numberInput = document.getElementById("number-input");
const convertBtn = document.getElementById("convert-btn");
const result = document.getElementById("result");
const checkUserInput = () = {
if (!numberInput.value || isNaN(parseInt(numberInput.value))) {
}
console.log(numberInput.value);
};
convertBtn.addEventListener("click", checkUserInput);
numberInput.addEventListener("keydown", (e) = {
if (e.key === "Enter") {
checkUserInput();
}
});
This is the part I’m talking about. I tried this
I think this might be correct, let me know if someone has something better.
if (!numberInput.value || isNaN(parseInt(numberInput.value)) || parseInt(numberInput.value) < 0) {
}
const checkUserInput = () = {
if (!numberInput.value || isNaN(parseInt(numberInput.value))) {
}