Having trouble displaying my number using javascript - javascript

I have to convert from decimal to binary and it requires at least 3 functions and it has to be displayed in HTML. This is what I've got so far and can't figure out how to make it display properly?
// Prompt user for number between 1-1000
let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
function store() {
let quotient = [];
let answer = quotient.reverse();
return answer;
}
function check() {
while (input != 0) {
if (input < 1000 && input % 2 != 0) {
return quotient.push("1");
input = input / 2;
}
else if (input < 1000 && input % 2 == 0) {
return quotient.push("0");
input = input / 2;
}
}
}
function display() {
document.getElementById("displayNumber").innerHTML = answer;
}
display();
<h1 id="displayNumber"></h1>

Here is the fixed script, I hope this helps.
function check(input, quotient) {
while (input != 0) {
if (input < 1000 && input % 2 != 0) {
quotient.push("1");
input = parseInt(input / 2);
}else if (input < 1000 && input % 2 == 0) {
quotient.push("0");
input = parseInt(input / 2);
}
}
}
function display() {
let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
let quotient = [];
check(input, quotient);
let answer = quotient.reverse().join('');
document.getElementById("displayNumber").innerHTML = answer;
}
display();

Currently, you are just creating the function store and check but not actually calling them.
However, if all you are wanting to do is to display the input as binary, you can use the toString function, passing in the base you desire.
Im not sure what you want to display if the number is outside of the range 1-1000. So I just put "Invalid input". You can add some more checks for if it is NAN ect
<!DOCTYPE html>
<html>
<h1 id="displayNumber"></h1>
<script>
// Prompt user for number between 1-1000
let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
function display() {
if (input < 1 || input > 1000) {
document.getElementById("displayNumber").innerHTML = "Invalid input";
} else {
document.getElementById("displayNumber").innerHTML = input.toString(2);
}
}
display();
</script>
</html>

Here is updated snippet :
function check(input) {
let quotient = [];
while (input != 0) {
if (input < 1000 && input % 2 != 0) {
input = input / 2;
}
else if (input < 1000 && input % 2 == 0) {
input = input / 2;
}
quotient.push(input);
}
return quotient;
}
function display() {
let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
var answer = check(input);
document.getElementById("displayNumber").innerHTML = answer;
}
display();
<h1 id="displayNumber"></h1>

In display function, you are assigning answer to innerHTML but not calling your check function.
<html>
<h1 id="displayNumber"></h1>
<script>
let input = parseInt(prompt('Enter a number between 1 and 1000', '50'));
function check(input) {
let quotient = [];
while (input > 0) {
quotient.push(input % 2);
input = parseInt(input / 2);
}
return quotient.reverse().join('');
}
function display() {
if (input >= 1 && input <= 1000) {
document.getElementById("displayNumber").innerHTML = check(input);
}
}
display();
</script>
</html>

Related

Javascript not rounding properly

I made a simple number guessing game in javascript which goes from 1 to 100.
My problem is that everything works, except when I type a decimal number from 100 to 101. I want my code to give me the "Please enter a number from 1 to 100" message then, but it doesn't.
How do I get Javascript to round this properly?
let randomNumber = Math.floor(Math.random() * 100); // Random number
function guess() {
let inp = document.getElementById("number").value; // Reading input
let inpRounded = Math.floor(inp);
// Random number is higher
if (inpRounded < randomNumber && inpRounded >= 1 && inpRounded <= 100) {
document.getElementById("result").innerHTML = "Higher";
}
// Random number is lower
else if (inpRounded > randomNumber && inpRounded >= 1 && inpRounded <= 100) {
document.getElementById("result").innerHTML = "Lower";
// Input matches random number
} else if (inpRounded == randomNumber && inpRounded >= 1 && inpRounded <= 100) {
document.getElementById("result").innerHTML = "You won!";
}
// Input is weird
else if (inpRounded == null) {
document.getElementById("result").innerHTML = "Please enter a valid number";
}
// Input is lower than 1 or higher than 100
else if (inpRounded < 1 || inpRounded > 100) {
document.getElementById("result").innerHTML = "Please enter a number from 1 to 100";
}
// Other issue
else {
document.getElementById("result").innerHTML = "Error"
}
}
<h2>Enter a number from 1 to 100</h2>
<input type="number" placeholder="Type your guess here" id="number">
<input type="button" value="Go!" onclick="guess()">
<p id="result"></p>
Don't round the number initially, otherwise you'll lose the decimal information you need when you want to reject values between 100 and 101.
function guess() {
const value = document.getElementById("number").value; // Reading input
const numValue = Number(value);
if (numValue < 1 || numValue > 100) {
document.getElementById("result").textContent = "Please enter a number from 1 to 100";
return;
}
const inpRounded = Math.floor(numValue);
// etc
Just put this code first before you check other things.
if (inpRounded == null) {
document.getElementById("result").innerHTML = "Please enter a valid number";
return;
}
if (inpRounded < 1 || inpRounded > 100) {
document.getElementById("result").innerHTML = "Please enter a number from 1 to 100";
return;
}

Limit content editable table data input to only one number with one decimal

I need to limit input to only one number and one decimal. For example to be able to put 9.5 but not 95.
Only thing I could think of is something like this:
let length = e.currentTarget.textContent.length;
if (length >= 3) {
console.log(length);
e.preventDefault();
}
Use this function
function numberValidator(input) {
if (input.length < 4)
if (typeof input == 'string') {
if (input.indexOf('.') == 0 || input.indexOf('.') == 2)
return false;
input = parseFloat(input).toFixed(1)
if (parseFloat(input) < 10) {
console.log(input);
return true
}
}
return false
}
if (numberValidator(e.currentTarget.textContent)) {
// setState
} else {
// e.preventDefault(); or dont set state
}
Let me know if it dosen't work,Upvote if works:-)

javascript multiplication table order

I have the followign school project:
You enter a number into a prompt
Highlights every occurance of numbers in interval [1; 100] that are the multiplicator of the inputted number.
Issue is, whenever I input a number dividable by /5 it ruins the table formatting with the extra <br/>
<html>
<body>
<script>
var y = prompt("enter a number between 1 -100 ")
if( y<=100 && y>=1 ){
for (i = 1 ; i<=100 ; i++){
var idk = i + " "
if (i%y!=0){
document.write(idk)
}
if(i%10==0 ){
document.write("</br>" )
console.log(i)
}
if(i%y==0){
document.write(idk.fontcolor("red"))
}
}
}
else {
document.write("your number isnt good")
}
</script>
</body>
this is my code can someone help me fix it
That's because of the order of your if conditions.
If your number is divided by 5, it also by 10. Re-arrange the code structure, so you check for color-ins first and only then apply <br/> like so. (even then, it is a kind of ugly conditional though)
Basically , just moved if (i % y == 0) {} above if (i % 10 == 0) {}
let y = prompt("enter a number between 1 -100 ")
if (y <= 100 && y >= 1) {
for (let i = 1; i <= 100; i++) {
let idk = i + " "
if (i % y != 0) {
document.write(idk)
}
if (i % y == 0) {
document.write(idk.fontcolor("red"))
}
if (i % 10 == 0) {
document.write("</br>")
}
}
} else {
document.write("your number isnt good")
}

Input number output corresponding value

Given these 9 words, display on the page the word corresponding to their chosen number
1.mercury
2.venus
3.earth
4.mars
5.jupiter
6.saturn
7.uranus
8.neptune
9.pluto
Im not sure what I'm missing here Ive done a lot of trial an error and nothing seems to work.
I've tried using numEntry as my comparison for all the if statements and it hasn't worked. When I made var numEntry = true; only Mercury would display. When I made var numEntry = 1,2,3,4,5,6,7,8,9 only pluto would show. I then tried to create a variable for each number and use each once in a comparison like below but every planet shows up instead of the corresponding number to planet.
var numberOfPlanet = prompt("Please enter a number between 1 and 9");
function thePlanets(){
var numOne = 1;
var numTwo = 2;
var numThree = 3;
var numFour = 4;
var numFive = 5;
var numSix = 6;
var numSeven = 7;
var numEight = 8;
var numNine = 9;
//do I need to define numberEntry if I use it in my comparisons below? what do I define it as after the = //// I tried defining as true but only mercury will appear, i tried inserting numbers 1 through 9 but only pluto worked//
if(numOne = 1 ){
document.write("mercury");
}
if(numTwo = 2 ){
document.write("venus");
}
if(numThree = 3 ){
document.write("earth");
}
if(numFour = 4 ){
document.write("mars");
}
if(numFive = 5 ){
document.write("jupiter");
}
if(numSix = 6 ){
document.write("saturn");
}
if(numSeven = 7 ){
document.write("uranus");
}
if(numEight = 8 ){
document.write("neptune");
}
if(numNine = 9 ){
document.write("pluto");
}
}
thePlanets();
I just need a number to correspond with the right planet when the user enters that number eg. ( user enters 1 and it displays mercury)
Some notes:
Use numberOfPlanet as the function argument to compare with (it becomes num inside the function).
Convert numberOfPlanet to Number as prompt() returns string.
Use === (strong comparison) instead of = (assignment).
Use else if instead of next if if you need only one variant from some so that the comparing stops when the right result is found.
var numberOfPlanet = Number(prompt("Please enter a number between 1 and 9"));
function thePlanets(num){
if(num === 1){
document.write("mercury");
}
else if(num === 2){
document.write("venus");
}
else if(num === 3){
document.write("earth");
}
else if(num === 4){
document.write("mars");
}
else if(num === 5){
document.write("jupiter");
}
else if(num === 6){
document.write("saturn");
}
else if(num === 7){
document.write("uranus");
}
else if(num === 8){
document.write("neptune");
}
else if(num === 9){
document.write("pluto");
}
}
thePlanets(numberOfPlanet);

JavaScript: Both dialog boxes show, even if the user enters incorrectly

This is a simple JavaScript/HTML guessing game. The only problem I have is that when the user enters a number/letter other than 1-6, the "error" message should pop-up — and it does, but then the game goes on and it still tells you whether you're a winner or not. This is the code that I have.
function jsFunc() {
var number = Math.ceil (Math.random() * 6) + 1;
var guessNum = 0;
guessNum = document.getElementById("num").value;
if (isNaN(guessNum) || guessNum < 1 || guessNum > 6) {
alert ("Must be a number between 1 and 6. Please re-enter!");
}
if (number == guessNum)
alert("Congratulations, You Win!!!");
else
alert("Aw, You Lose..");
}
You can just add if else to make it work
Please refer snippet
function jsFunc() {
var number = Math.ceil (Math.random() * 6) + 1;
var guessNum = 0;
guessNum = document.getElementById("num").value;
if (isNaN(guessNum) || guessNum < 1 || guessNum > 6) {
alert ("Must be a number between 1 and 6. Please re-enter!");
}else if (number == guessNum){
alert("Congratulations, You Win!!!");
}
else{
alert("Aw, You Lose..");
}
}
<input id ="num"/> <button onClick="jsFunc()">Submit</button>
You should be doing else-if
function jsFunc() {
var number = Math.ceil (Math.random() * 6) + 1;
var guessNum = 0;
guessNum = document.getElementById("num").value;
if (isNaN(guessNum) || guessNum < 1 || guessNum > 6) {
alert ("Must be a number between 1 and 6. Please re-enter!");
}
else if (number == guessNum)
alert("Congratulations, You Win!!!");
else
alert("Aw, You Lose..");
}
if (isNaN(guessNum) || guessNum < 1 || guessNum > 6) {
alert ("Must be a number between 1 and 6. Please re-enter!");
}
else if (number == guessNum)
{
alert("Congratulations, You Win!!!");
}
else
{
alert("Aw, You Lose..");
}
You if/else logic needs to be one continuous block. Because in your original code, your second IF get evaluated no matter what!

Categories