如何执行循环,直到用户选择取消提示?

时间:2016-06-15 04:01:19

标签: javascript loops for-loop ascii do-while

我正在编写一个程序,其中用户输入被分解为字母,然后显示为ASCII字符。

用户可以一遍又一遍地在提示框中输入输入,直到他选择Cancel,按Enter键或Esc键。

例如,如果您输入“Hello'其次是' Bye'出现提示时,第3次只需按Enter键而不键入任何内容,您应该看到:

Word entered is "Hello"
H = 72
e = 101
l = 108
l = 108
o = 111

Word entered is "Bye"
B = 66
y = 121
e = 101

我的问题是

1)输入的单词是......"行没有出现,

2)提示信息只执行一次,无论用户是否留空。

这是我的代码:

<script>
    do {
        var word = prompt ("Please enter a word of your choice");
        if (word == null)
            document.getElementById("myDiv").innerHTML = "Word entered is " + "\"" + word + "\"";
        else
            break;
    } while (word == null);

    var letter = "<ul>";

    for (var i = 0; i < word.length; i++){
        letter += word[i] + " = " + word.charCodeAt(i) + "<br>";
    }

    letter += "</ul>";
    document.getElementById("myDiv").innerHTML = letter;
</script>

4 个答案:

答案 0 :(得分:0)

您正在检查他们是否没有输入单词然后您正在显示单词变量。如果它不为空,您只想显示他们选择的单词。

答案 1 :(得分:0)

查看代码https://jsfiddle.net/q41gm0j6/

基本上你错过了回归;

  (function(){
    do {
     var word = prompt ('Please enter a word of your choice');
       if (word === '' || word === null){
         word = '';
         var t = document.getElementById('myDiv');
         t.innerHTML = 'Word entered is' + word;
         return;
        }
        else break;
    } while (word === null);
      var letter = '<ul>';
      for (var i = 0; i < word.length; i++){
        letter += word[i] + ' = ' + word.charCodeAt(i) + '<br>';
      }
      letter += '</ul>';
      document.getElementById('myDiv').innerHTML += letter;
   })();

答案 2 :(得分:0)

我认为你需要分开&#34;单词&#34; by&#34; &#34;,并列出每个单词的字母。

&#13;
&#13;
// this function make the variables not glabel.
(function() {
  var words = '';

  while (words === '') {
    // get the input until something not empty or 'Esc'
    words = prompt("Please enter a word of your choice");
  };

  // this function will return the HTML which will list the letters of the word
  var listLetters = function(word) {
    letters = "";
    for (var i = 0; i < word.length; i++) {
      letters = letters + word[i] + " = " + word[i].charCodeAt() + "<br>";
    }
    return letters;
  }

  // get target
  var t = document.getElementById("myDiv");
  // empty the target
  t.innerHTML = '';

  // process the inputs
  words.split(' ').forEach(function(word) {
    // print the word
    t.innerHTML += 'Word entered is "' + word + '"<br>';
    // list the letters
    t.innerHTML += listLetters(word) + '<br>';
  });

})();
&#13;
<div id='myDiv'></div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

检查此plunker代码,我为您创建了实现所需功能的代码。希望这会对你有所帮助。

你的问题1的原因是你要覆盖div的innerHTML,为什么&#34; Word输入的是......&#34;行没有出现

https://plnkr.co/edit/RN0yNxDAsTSQQeSvGYb9?p=info

window.onload = function() {
var word = "";
do {
    word = prompt ("Please enter a word of your choice");

     if(word !== null){

     var span = document.createElement("span");

          span.innerHTML = "Word entered is " + "\"" + word + "\"";

          document.getElementById("myDiv").appendChild(span);


          var ul = document.createElement("ul");

          var letter = "<ul>";

          for (var i = 0; i < word.length; i++){
              letter += word[i] + " = " + word.charCodeAt(i) + "<br>";
          }

          letter += "</ul>";


          ul.innerHTML = letter;

          document.getElementById("myDiv").appendChild(ul);
}

} 

while (word !== null);

}