javascript - Get and set in js using function constructors -
this code
function choices(questions, options, yourchoices) { this.questions = questions; this.options = options; this.yourchoices = yourchoices; }; choices.prototype.displaychoices = function() { console.log(this.questions) (var = 0; < this.options.length; i++) { console.log(i + ':' + this.options[i]); } } choices.prototype.getchoices = function(opt) { if (opt == this.yourchoices) { console.log('wow,' + this.options + ' favaroite!'); } else { console.log('choose wisely') } } var c1 = new choices('which food?', ['paneer', 'mutton', 'chicken'], ['1', '2', '3']) var c2 = new choices('which 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 of options..')); choices[n].getchoices(getchoice); well tried choose wisely , condition in if statement false... should please help!! thank you:)
the problem due line:
if(opt == this.yourchoices) this.yourchoices array cant equal single number i-e opt. remove condition , code work. if user enter wrong number this.options[opt] undefined. hence make check. if(this.options[opt]) check whether undefined or not.
function choices(questions,options,yourchoices) { this.questions = questions; this.options = options; this.yourchoices = yourchoices; }; choices.prototype.displaychoices = function(){ console.log(this.questions) for(var = 0; < 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]+' favaroite!'); }else{ console.log("choose wisely"); } } var c1 = new choices('which food?', ['paneer','mutton','chicken'], ['1','2','3'] ) var c2 = new choices('which 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 of options..')); choices[n].getchoices(getchoice);
Comments
Post a Comment