2015-10-17 3 views
0

Мне нужно создать программу, в которой вы вводите номер 1-100, и выводит класс соответствия, соответствующий вашему счету, и у меня возникли проблемы с отображением приглашения. Вот код:HTML-подсказка() не отображает

function myGrade() { 
    var Input = prompt("Input grade here:"); 
    if (Input >= 90) { 
    document.write("The score you entered is " 
     Input ". Your letter grade is A."); 
    } else if (Input >= 80 && Input < 90) { 
    document.write("The score you entered is " 
     Input ". Your letter grade is B."); 
    } else if (Input >= 70 && Input < 80) { 
    document.write("The score you entered is " 
     Input ". Your letter grade is C."); 
    } else if (Input >= 60 && Input < 70) { 
    document.write("The score you entered is " 
     Input ". Your letter grade is D."); 
    } else if (Input < 60) { 
    document.write("The score you entered is " 
     Input ". Your letter grade is F."); 
    } 
} 

Я использую <body onload = "myGrade()"> выше этот фрагмент кода.

+0

Какие ошибки вы обнаружили на консоли? –

ответ

0

Это работает: Скопируйте этот код в файл и загрузите его с помощью браузера.

<!DOCTYPE html> 
<html class=""> 

<head> 
<script type = "text/javascript" > 
    function myGrade() {  
    var Input = prompt("Input grade here:",50); 
    if (Input >= 90) { 
     document.write("The score you entered is " 
     + Input + ". Your letter grade is A."); 
     } else if (Input >= 80 && Input < 90) { 
     document.write("The score you entered is " 
     + Input + ". Your letter grade is B."); 
    } else if (Input >= 70 && Input < 80) { 
     document.write("The score you entered is " 
     + Input + ". Your letter grade is C."); 
    } else if (Input >= 60 && Input < 70) { 
     document.write("The score you entered is " 
     + Input + ". Your letter grade is D."); 
    } else if (Input < 60) { 
     document.write("The score you entered is " 
     + Input + ". Your letter grade is F."); 
    } 

    } </script> 
</head> 
<body onload = "myGrade()"> 
<br> 
</body> 
</html> 
+0

В чем была проблема и что вы изменили? –

0

Вам необходимо установить приглашение первой:

npm install prompt 

Таким образом, решение немного сложнее, чем у вас:

var prompt = require('prompt'); 

prompt.start(); 

prompt.get(['grade'], function (err, Input) { 
     if (err) { return onErr(err); } 
     if (Input.grade >= 90) { 
      console.log("The score you entered is " + 
      Input.grade + ". Your letter grade is A."); 
     } else if (Input.grade >= 80 && Input.grade < 90) { 
      console.log("The score you entered is " 
      + Input.grade + ". Your letter grade is B."); 
     } else if (Input.grade >= 70 && Input.grade < 80) { 
      console.log("The score you entered is " 
      + Input.grade + ". Your letter grade is C."); 
     } else if (Input.grade >= 60 && Input.grade < 70) { 
      console.log("The score you entered is " 
      + Input.grade + ". Your letter grade is D."); 
     } else if (Input.grade < 60) { 
      console.log("The score you entered is " 
      + Input.grade + ". Your letter grade is F."); 
     } 
}); 

function onErr(err) { 
    console.log(err); 
    return 1; 
} 

Вы можете запустить сценарий с помощью этой команды:

node script.js 

More information.

+0

'npm prompt' - это что-то для запроса ввода из командной строки для приложений узлов. Этот вопрос не был помечен [tag: nodejs]. Я думаю, что OP запускает свою программу в браузере. –

Смежные вопросы