获取和使用功能构造

问题描述:

这是我的代码获取和使用功能构造

function Choices(questions, options, yourChoices) { 
 
    this.questions = questions; 
 
    this.options = options; 
 
    this.yourChoices = yourChoices; 
 
}; 
 

 
Choices.prototype.DisplayChoices = function() { 
 
    console.log(this.questions) 
 
    for (var i = 0; i < this.options.length; i++) { 
 
    console.log(i + ':' + this.options[i]); 
 
    } 
 
} 
 

 
Choices.prototype.getChoices = function(opt) { 
 
    if (opt == this.yourChoices) { 
 
    console.log('Wow,' + this.options + ' is your favaroite!'); 
 
    } else { 
 
    console.log('Choose Wisely') 
 
    } 
 
} 
 

 
var c1 = new Choices('Which is your most likely Food?', ['Paneer', 'Mutton', 'Chicken'], ['1', '2', '3']) 
 

 
var c2 = new Choices('Which is your most likely Color?', ['Orange', 'red', 'blue'], ['1', '2', '3']) 
 

 
var choices = [c1, c2]; 
 
var n = Math.floor(Math.random() * choices.length); 
 
choices[n].DisplayChoices(); 
 
var getChoice = parseInt(prompt('Please Select any of your Options..')); 
 
choices[n].getChoices(getChoice);

以及试图做到这一点的JS设置,但我总是明智地选择和if语句始终为假的情况.. 。我该怎么做,请帮助!谢谢:)

的问题是由于这一行:

if(opt == this.yourChoices) 

this.yourChoices是一个阵列,其不能是等于一个单一的数字I-E 选择。删除这个条件,你的代码将工作。如果用户输入错误的号码,那么this.options [opt]是undefined。因此做一个检查。 if(this.options[opt])来检查它是否未定义。

function Choices(questions,options,yourChoices) { 
 
    this.questions = questions; 
 
    this.options = options; 
 
    this.yourChoices = yourChoices; 
 
}; 
 
Choices.prototype.DisplayChoices = function(){ 
 
    console.log(this.questions) 
 
    for(var i = 0; i < this.options.length; i++){ 
 
     console.log(i +':'+ this.options[i]); 
 
    } 
 
} 
 
Choices.prototype.getChoices = function(opt){ 
 
\t \t \t \t if(this.options[opt]){ 
 
     console.log('Wow,'+this.options[opt]+' is your favaroite!'); 
 
     }else{ 
 
     console.log("choose wisely"); 
 
     } 
 
     
 
    } 
 

 
var c1 = new Choices('Which is your most likely Food?', 
 
        ['Paneer','Mutton','Chicken'], 
 
        ['1','2','3'] 
 
        ) 
 

 
var c2 = new Choices('Which is your most likely Color?', 
 
        ['Orange','red','blue'], 
 
        ['1','2','3'] 
 
        ) 
 

 
var choices = [c1, c2]; 
 
var n = Math.floor(Math.random() * choices.length); 
 
choices[n].DisplayChoices(); 
 
var getChoice = parseInt(prompt('Please Select any of your Options..')); 
 
    choices[n].getChoices(getChoice);

+0

嘿其工作,但可以üPLZ简单介绍一下,如果(this.options [选择])实际上呢?... –

+0

@ShashankMadan如果(this.options [选择])是什么检查它是否未定义。如果用户输入任何超出界限的选项,它将返回false。在你的情况下,当数字不是0,1,2时它返回false。 –

+0

非常感谢这么多先生,真的非常有帮助,谢谢!:) –

你应该改变你的getChoices功能类似下面我想

Choices.prototype.getChoices = function(opt){ 
    if(opt <= this.options.length && opt >=0){ 
     console.log('Wow,'+this.options[opt]+' is your favaroite!'); 
    }else{ 
     console.log('Choose Wisely') 
    } 
} 

function Choices(questions,options,yourChoices) { 
    this.questions = questions; 
    this.options = options; 
    this.yourChoices = yourChoices; 
}; 
Choices.prototype.DisplayChoices = function(){ 
    console.log(this.questions) 
    for(var i = 0; i < this.options.length; i++){ 
     console.log(i +':'+ this.options[i]); 
    } 
} 
Choices.prototype.getChoices = function(opt){ 
console.log(opt,this.yourChoices); 
     if(this.yourChoices[opt]){ // (opt == this.yourChoices) this condition is not right because opt is integer and this.yourChoices is object 
     console.log('Wow,'+this.options[opt-1]+' is your favaroite!'); 
    }else{ 
     console.log('Choose Wisely') 
    } 
    } 

var c1 = new Choices('Which is your most likely Food?', 
        ['Paneer','Mutton','Chicken'], 
        ['1','2','3'] 
        ) 

var c2 = new Choices('Which is your most likely Color?', 
        ['Orange','red','blue'], 
        ['1','2','3'] 
        ) 

var choices = [c1, c2]; 
var n = Math.floor(Math.random() * choices.length); 
choices[n].DisplayChoices(); 
console.log(n); 
var getChoice = parseInt(prompt('Please Select any of your Options..')); 
    choices[n].getChoices(getChoice); 

这里有两种问题:

  1. 你选择的选择与比较整个阵列的选择,所以你应该改变这条线

    if(opt == this.yourChoices){

    与以下

    if(this.yourChoices.indexOf(opt) > -1){

  2. 你充满字符串数组(如: '1', '2',...),所以你不必解析为int用户的选择,所以更改如下:

    var getChoice = parseInt(prompt('Please Select any of your Options..'));

    var getChoice = prompt('Please Select any of your Options..');

我希望它能帮助你,再见。