-
-
Notifications
You must be signed in to change notification settings - Fork 43.7k
Closed
Labels
js v9 certThis is for the JS V9 certification.This is for the JS V9 certification.scope: curriculumLessons, Challenges, Projects and other Curricular Content in curriculum directory.Lessons, Challenges, Projects and other Curricular Content in curriculum directory.status: PR in worksWork in Progress (WIP) Issues.Work in Progress (WIP) Issues.
Description
Describe the Issue
STEPS TO REPRODUCE:
- Add a negative number to your app
RESULT:
Negative numbers are displayed as isNaN = false but the app displays NaN on the result field.
The user can see on the console if the application running through the website of freeCodeCamp
Potential infinite loop detected on line 32. Tests may fail if this is not changed.
If I copy html, css and js file to vc and run it stand-alone on the browser I see this on the console
Uncaught RangeError: Maximum call stack size exceeded
at decimalToBinary (script.js:33:3)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
at decimalToBinary (script.js:36:12)
decimalToBinary @ script.js:33
decimalToBinary @ script.js:36
decimalToBinary @ script.js:36
decimalToBinary @ script.js:36
decimalToBinary @ script.js:36
...
Affected Page
Your code
const numberInput = document.getElementById("number-input");
const convertBtn = document.getElementById("convert-btn");
const result = document.getElementById("result");
const animationContainer = document.getElementById("animation-container");
const animationData = [
{
inputVal: 5,
marginTop: 300,
addElDelay: 1000,
msg: 'decimalToBinary(5) returns "10" + 1 (5 % 2). Then it pops off the stack.',
showMsgDelay: 15000,
removeElDelay: 20000,
},
{
inputVal: 2,
marginTop: -200,
addElDelay: 1500,
msg: 'decimalToBinary(2) returns "1" + 0 (2 % 2) and gives that value to the stack below. Then it pops off the stack.',
showMsgDelay: 10000,
removeElDelay: 15000,
},
{
inputVal: 1,
marginTop: -200,
addElDelay: 2000,
msg: 'decimalToBinary(1) returns "1" (base case) and gives that value to the stack below. Then it pops off the stack.',
showMsgDelay: 5000,
removeElDelay: 10000,
}
];
const decimalToBinary = (input) => {
if (input === 0 || input === 1) {
console.log("input ", input)
console.log("input === 0 ", input === 0)
console.log("input === 1 ", input === 1)
return String(input);
} else {
const result = decimalToBinary(Math.floor(input / 2)) + (input % 2);
console.log("result = >>>>>> ", result)
return result
}
};
const showAnimation = () => {
result.innerText = "Call Stack Animation";
animationData.forEach((obj) => {
setTimeout(() => {
animationContainer.innerHTML += `
<p id="${obj.inputVal}" style="margin-top: ${obj.marginTop}px;" class="animation-frame">
decimalToBinary(${obj.inputVal})
</p>
`;
}, obj.addElDelay);
setTimeout(() => {
document.getElementById(obj.inputVal).textContent = obj.msg;
}, obj.showMsgDelay);
setTimeout(() => {
document.getElementById(obj.inputVal).remove();
}, obj.removeElDelay);
});
setTimeout(() => {
}, 20000);
};
const checkUserInput = () => {
const inputInt = parseInt(numberInput.value);
console.log("inputInt=>>>>>", inputInt, " isNaN =>>>> ",isNaN(inputInt))
if (!numberInput.value || isNaN(inputInt)) {
alert("Please provide a decimal number");
return;
}
if (inputInt === 5) {
showAnimation();
return;
}
result.textContent = decimalToBinary(inputInt);
numberInput.value = "";
};
convertBtn.addEventListener("click", checkUserInput);
numberInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
checkUserInput();
}
});
Expected behavior
As far as I read on Google negative numbers can be converted to binary but it's the whole process,
Here is a clear example of how to do it at the bottom of the article: https://sciencing.com/calculate-binary-numbers-8267989.html
Screenshots
System
Device: Laptop
OS: Windows 11 Home
Browser: Chrome
Version: 124.0.6367.61
Additional context
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
js v9 certThis is for the JS V9 certification.This is for the JS V9 certification.scope: curriculumLessons, Challenges, Projects and other Curricular Content in curriculum directory.Lessons, Challenges, Projects and other Curricular Content in curriculum directory.status: PR in worksWork in Progress (WIP) Issues.Work in Progress (WIP) Issues.

