使用函数构造函数在js中获取和设置

时间:2017-07-26 10:04:57

标签: javascript

这是我的代码



AlarmManager




很好地尝试这样做,但我总是明智地选择,if语句中的条件总是假的......我该怎么办请帮助!!谢谢你:))

3 个答案:

答案 0 :(得分:0)

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 :(得分:0)

有两个问题:

  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..');

  3. 我希望它可以帮助你,再见。

答案 2 :(得分:0)

问题是由于这一行:

if(opt == this.yourChoices)

this.yourChoices 是一个不能等于单个数字的数组i-e opt 。删除此条件,您的代码将工作。如果用户输入错误的号码,则this.options [opt] 未定义。因此,请检查。 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){
				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);