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")
}
Related
I want to have a logic where if I enter an even number I want next 10 even numbers to be printed and If I enter an odd number, I want next 10 odd numbers to be printed. How should I rectify this logic inside a function. If someone can please help me rectifying the logic which was answered.
JS
function oddEven() {
var input = prompt("");
for (let x = 1; x <= 10; x++) {
console.log(input + x * 2);
}
}
oddEven()
This should work for any number
const input = 2
for (let x = 1; x <= 10; x += 1) {
console.log(input + x * 2)
}
i hope this help
function printTen(input){
let list = []
let number = input
while(list.length <= 10){
number++
if(input % 2 === 0 && number % 2 === 0){
console.log(number + " is even");
list.push(number)
}else if(input % 2 !== 0 && number % 2 !== 0) {
console.log(number + " is odd");
list.push(number)
}
}
}
printTen(9)
let inputval = 2;
for (let x = 1; x <= 10; x++) {
console.log(inputval + x * 2)
}
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!
var number = prompt("Typ some numbers:")
var som = 0
for (var x = 0; x < number.length; x++) {
if (!(number[x] === 0)) {
if ((number[x] % 2) === 1) {
som += (number[x] * number[x])
}
} else {
break;
}
}
alert(som)
I want to type some random numbers (0-9) and then it must say the som of the Square of all the odd numbers before I type zero. For example I type: 5903. 5*5 + 9*9 = 106. So AFTER I type a zero it must STOP the for loop of going further. But right now if I typ 5903 it says 115, so right now it still DOES count 3*3 extra. So how do I make it stop after I type a zero? It doesn't work right now, it goes on after I type a zero. Someone know what's the problem? Maybe syntax?
Please change
if (!(number[x] === 0)) {
to
if (number[x] !== '0') {
because you are comparing strings.
Working example:
var number = prompt("Typ some numbers:"),
som = 0;
for (var x = 0; x < number.length; x++) {
if (number[x] !== '0') {
if ((number[x] % 2) === 1) {
som += (number[x] * number[x]);
}
} else {
break;
}
}
alert(som);
you are checking if the number is equal to 0, but your number is actually '0'
you need to parse the input as an integer or you can use double equals
var number = prompt("Typ some numbers:");
var som = 0;
for (var x = 0; x < number.length; x++){
if(number[x] == 0){
break;
}
if ((number[x] % 2) === 1) {
som += (number[x] * number[x]);
}
}
alert(som);
After a for loop,
example: for( int number = 1 ; number < 100 ; number++ )
in which the multiples 3 print boo and multiples of 5 print casper and multiples of 3 and 5 print boocasper,and everything else as the original number. How would you write a code to print these values, 10 per line.
I figured out the coding to output the values of these multiples,I just need help figuring out printing ten of these values/phrases per line.
You are looking for something like this:
for (var i = 1; i < 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
document.write("boocasper ");
} else if (i % 3 == 0) {
document.write("boo ");
} else if (i % 5 == 0) {
document.write("casper ");
}else{
document.write(i+" ");
}
if (i % 10==0) {
document.write("<br/>");
}
}
I am making a simple 1 - 10 guess the number game with Javascript.
It can be viewed here Guessing game
To add up score i have a var score = 4 which de-increments each time the number guessed (with a for loop which is smaller than 5) incorrect. I += to var tally and display tally as score.
My problem is score always equals 0, and therefore does not add anything to tally, I am struggling to find a solution.
My javascript is:
var tally;
function play() {
var compNum = (Math.random() * 10).toFixed(0);
var score = 4;
for (var i = 0; i < 4; i++) {
if (i == 0) {
var userNum = prompt("Enter a number between 1 and 10");
} else {
if (userNum < compNum) {
userNum = prompt("Guess higher, you have " + (4 - i) + " turns left ", userNum);
} else if (userNum > compNum) {
userNum = prompt("Guess lower you have " + (4 - i) + " turns left ", userNum);
}
}
score--;
}
tally += score;
$("#score").html("score: " + tally);
if (i >= 3 && userNum != compNum) {
var again = confirm("Sorry you lost. The number was: " + compNum + " Play again?");
} else if (userNum == compNum) {
again = confirm("Well done! play again?");
i <= 5;
}
if (again) {
play();
}
if (userNum == "") {
i <= 5;
}
}
HTML:
<button onclick="play()">PLAY</button>
<div id="score"></div>
Your help is really appreciated
You should check whether the number entered by the use is equal to the random one and if that is the case exit form the for loop.
With your code the loop runs the whole 4 times,
In your loop, you need to check if the user has the right answer and exit the loop if they do.
for (var i = 0; i < 4; i++) {
if (i == 0) {
var userNum = +prompt("Enter a number between 1 and 10");
} else {
if (userNum < compNum) {
userNum = prompt("Guess higher, you have " + (4 - i) + " turns left ", userNum);
} else if (userNum > compNum) {
userNum = prompt("Guess lower you have " + (4 - i) + " turns left ", userNum);
}
}
if (userNum === compNum) {
// they guessed right, so exit the loop
break;
}
score--;
}
In addition, I'd check to see if isNaN(userNum) to check your user actually entered a number. It's up to you if you want to give them another chance if they don't.