0

The third button is supposed to Bring up a prompt in which you can type in a number from 1-3 and if you type something else it should say that what you typed in is not a number between 1 and 3, but if you type 1/2/3 it should bring up a prompt in which you can type your text, and the text that is under 1/2/3 should change into what you typed. Instead, when you type in something not from 1 to 3 in the first prompt it still brings up the second one(which it should not), and no matter what you type in the first prompt it will only change the first text. Sorry if it's just a dumb mistake, i spent hours trying to figure it out.

function HI()
{
    var A = prompt("Choose Text(1-3)")
    if(A = 1)
    {
        A=document.getElementById("1");
        var B = prompt("Type Your Text")
        A.innerHTML=B;
    }
    else if(A = 2)
    {
        A=document.getElementById("2");
        var B = prompt("Type Your Text")
        A.innerHTML=B;
    }
    else if(A = 3)
    {
        A=document.getElementById("3");
        var B = prompt("Type Your Text")
        A.innerHTML=B;
    }
    else
    {
        alert("What you typed is not a between 1 and 3")
    }
}
2
  • 1
    = is assignment, == and === are comparison Commented May 9, 2016 at 18:33
  • You are not performing a comparison in your if-statements but an assignment. Go and look up JavaScript operators in your textbook. Commented May 9, 2016 at 18:33

1 Answer 1

1

You need the equality indicator, not the assignment operator. Use A == 1 or B == 1 or whatever.

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

6 Comments

Knew it was a dumb mistake, mentioned it too
@RichardNalbandyan Don't worry about it, I've made plenty too :-). We all do, otherwise we aren't developers
Writing Yoda conditions is a good way to prevent repeating this in the future; place the constant on the left side of the equality and if you inadvertently use an assignment you'll get an error which should be easy to trace.
Thanks, I've never heard that term before, but I instantly like it :-)
Be warned, many people don't like it as it reduces readability; personally I have no problems reading Yodas.
|

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.