0

I wanted the user to input a number and if the number is a multiple of 3 or 5, I want a word/paragraph that says, "You are a witch"

created a function but I cannot seem to make it work

var x;

function pop() {
  for (x = 1; x <= 100; x++)
    if (x % 3 == 0) {
      document.getElementById('onClick').innerHTML = 'You are a Warrior';
    } else if (x % 5 == 0) {
      document.getElementById('onClick').innerHTML = 'You are a wizard';
    } else if (x % 3 == 0 && x % 5 == 0) {
      document.getElementById('onClick').innerHTML = 'You are a Sage';
    } else {
      document.getElementById('onClick').innerHTML = 'invalid';
    }
}
<input type="number" id="x">
<br>
<br>
<center><button onclick="pop()">Click Me</button> </center>  
<br>
<br>
<p id="onClick"></p>
3
  • 2
    check if (x % 3 == 0 && x % 5 == 0) first Commented Apr 10, 2019 at 21:45
  • 2
    Get rid of the loop. Just set x to the contents of the text box. Commented Apr 10, 2019 at 21:46
  • 1
    by the way, your code does not accept input from a user, it loops 100 times overwriting a single elements content every time Commented Apr 10, 2019 at 21:47

1 Answer 1

1

You never read the user input. You have a loop where there should not be one. You need to change the order of the comparisons.

function pop(){
    var x = document.getElementById("x").value;
    
    if (x % 3 == 0 && x % 5 == 0) {
        document.getElementById('onClick').innerHTML = "You are a Sage";
    }
    else if (x % 3 == 0 ) {
        document.getElementById('onClick').innerHTML = "You are a Warrior";
    }
    else if (x % 5 == 0){
        document.getElementById('onClick').innerHTML = "You are a wizard";
    }
    else {
        document.getElementById('onClick').innerHTML = "invalid";
    } 
}
<input type="number" id="x">
<br>
<br>
<center><button onclick="pop()">Click Me</button> </center>  
<br>
<br>
<p id="onClick"></p>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.