第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

實現(xiàn)三元運算符創(chuàng)建無限循環(huán)

實現(xiàn)三元運算符創(chuàng)建無限循環(huán)

躍然一笑 2021-11-12 16:19:01
    這是JS:function Question(question, answers, correct) {  this.question=question;  this.answers=answers;  this.correct=correct;}Question.prototype.displayQuestion=function () {  console.log(this.question)  this.answers.forEach((answer, i)=> {    console.log(`${i}: ${answer}`)  })}Question.prototype.checkAnswer=function(answer) {  (answer==this.correct)? true: false}var q1=new Question("What day is it today?", ["Monday", "Tuesday"], 0)var q2=new Question("How are you?", ["Good", "Really good"], 1)var q3=new Question("Is it nice out?", ["Yes", "No", "In-between"], 3)var questions=[q1, q2, q3];function createGame() {  var num=Math.floor(Math.random()*questions.length)  questions[num].displayQuestion()  var userInput=parseInt(prompt("Please enter answer"))  questions[num].checkAnswer(userInput)? console.log('You won!'): createGame()}createGame()這是 index.html<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>Section 5: Advanced JavaScript: Objects and Functions</title>    </head>    <body>        <h1>Section 5: Advanced JavaScript: Objects and Functions</h1>        <script src="script.js"></script>    </body></html我對這個簡單程序的目標是不斷輸出問題,直到用戶做對為止。如果用戶答對了問題,您將使用 console.log("You won!") 并且程序?qū)⑼V惯\行。我在三元運算符的幫助下做到了這一點。但是,為什么我的程序會一直運行?
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經(jīng)驗 獲得超5個贊

checkAnswer總是返回 undefined 因為你沒有做return任何事情:


Question.prototype.checkAnswer=function(answer) {

  return answer==this.correct

}


查看完整回答
反對 回復(fù) 2021-11-12
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號