2016-10-17 3 views
-1

Я работаю над кодом для сайта, пытающегося сделать ввод и на основе ввода отобразить конкретное сообщение. Всякий раз, когда я ввожу число, однако он устанавливается в первый, если результат. Любая помощь будет здорово, спасибоПеременная не меняется в случае если оператор

<form name="testsDone"> 
    How many of your tests have you taken? <input type ="text" id="tookTests" /><br /> 
</form> 


<script> 
function testCheck(){ 
    var a = parseFloat(document.getElementById("tookTests").value); 
    var b = a; 
    var c; 

    if (b = 2) { 
    c = "Yes you can! You've earned it."; 
    } else if (b = 1) { 
    c = "You need to keep studying then"; 
    } 
    else if (b > 2) { 
    c = "That's more tests than you're supposed to have"; 
    }else { 
    c = "Why are you on here? You need to study."; 
} 

document.getElementById("change").innerHTML = c; 
} 
    </script> 
    <button onclick="testCheck()">Check and See</button> 
    <p> Can you go to sleep?</p> 
    <p id="change"></p> 
+0

Равенство есть '==' или '===', а не '=' – joews

+0

= is присваивание, == сравнение http://jshint.com/ – epascarello

+0

'==' для сравнения, '=' для назначения – Li357

ответ

0

Вы назначаете значение здесь ..

если (Ь = 2) {

это должно быть ..

если (б == 2) {

+0

почему проголосовать !? – jay

-1

Эй изменить его к следующему

function testCheck(){ 
    var a = parseFloat(document.getElementById("tookTests").value); 
    var b = a; 
    var c; 

    if (b == 2) { 
    c = "Yes you can! You've earned it."; 
    } else if (b == 1) { 
    c = "You need to keep studying then"; 
    } 
    else if (b > 2) { 
    c = "That's more tests than you're supposed to have"; 
    }else { 
    c = "Why are you on here? You need to study."; 
} 

document.getElementById("change").innerHTML = c; 
} 
Смежные вопросы