需要在javascript中的条件语句之前输入

时间:2018-03-01 22:03:47

标签: javascript if-statement

在本练习中,我有一个提示,我想要输入。因此,如果用户没有输入任何内容,它将再次询问,直到给出响应。当最终给出响应时,它将通过条件语句列表。如果在第一个提示上给出响应,它将只是通过条件语句列表。我似乎无法让这个工作。我在这做错了什么?



var question =  prompt("What programming language are you currently learning?"); 
var questionUpper = question.toUpperCase();
var firstCap =  question.charAt(0).toUpperCase() + question.slice(1).toLowerCase();
while (true) {
    if (question === '') {
      var test = prompt("Please enter a programming language.");
    } else {
      break;
    } 
  if (questionUpper === 'HTML') {
      alert(questionUpper + ' is a good language to start with.');
    } else if (questionUpper === 'CSS') {
      alert(questionUpper + ' helps HTML look good.');
    } else if (firstCap === 'Javascript') {
      alert(firstCap + ' is a fun language to learn after HTML and CSS.');
    } else if (firstCap === 'Python') {
      alert(firstCap + ' is a good language, too.');
    } else  {
      alert("At least you're learning something.");	
    }
}




3 个答案:

答案 0 :(得分:0)

我相信错误是在while循环中。您正在为变量" test"分配答案,但条件是检查"问题"的值。



var question =  prompt("What programming language are you currently learning?"); 

while (true) {
    if (question === '') {
        question = prompt("Please enter a programming language.");
    } else {
        break;
    }
}

var questionUpper = question.toUpperCase();
var firstCap =  question.charAt(0).toUpperCase() + question.slice(1).toLowerCase();

if (questionUpper === 'HTML') {
    alert(questionUpper + ' is a good language to start with.');
} else if (questionUpper === 'CSS') {
    alert(questionUpper + ' helps HTML look good.');
} else if (firstCap === 'Javascript') {
    alert(firstCap + ' is a fun language to learn after HTML and CSS.');
} else if (firstCap === 'Python') {
    alert(firstCap + ' is a good language, too.');
} else  {
    alert("At least you're learning something.");   
}




我还在循环之后移动了大写字母以使其生效。

答案 1 :(得分:0)

要达到预期效果,您可以使用开关案例选项

var question =  prompt("What programming language are you currently learning?"); 
var questionUpper = question.toUpperCase();
var firstCap =  question.charAt(0).toUpperCase() + question.slice(1).toLowerCase();

switch(questionUpper){
  case 'HTML':
    alert(questionUpper + ' is a good language to start with.');
    break;
 case 'CSS':
    alert(questionUpper + ' is a good language to start with.');
    break;
 case 'JAVASCRIPT':
    alert(questionUpper + ' is a good language to start with.');
    break;
 case 'PYTHON':
    alert(questionUpper + ' is a good language to start with.');
    break;
  default:
     alert("At least you're learning something.");  
}

The sample in CodePen

答案 2 :(得分:0)

使用语言对象表示来解决这个问题看起来更有意义(也使得理解和编辑更容易)。例如:

var langs = {
  html: 'HTML is a good language to start with.',
  css: 'CSS helps HTML look good.',
  javascript: 'Javascript is a fun language to learn after HTML and CSS',
  python: 'Python is a good language, too.'
}

// ask initial question
var answer =  prompt('What programming language are you currently learning?')

// ask follow up until answer is given
while(answer.length <= 0){
  answer = prompt('Please enter a programming language.')
}

// deal with answer
var lower = answer.toLowerCase()
if(langs[lower]){
  alert(langs[lower])
}else{
  alert('At least you\'re learning something.')
}

通过这种方式,您可以修改langs对象以包含您喜欢的每种语言的任何属性。也许他们有与其他问题相关的文本,或者您希望与每种语言关联的其他变量。这样可以更轻松地添加或删除语言,或者从视图中分离数据,从而使您可以轻松地从数据库中获取语言数据,而无需更改任何逻辑。