Question

Hello fellow programmers :D I have a little issue, this is a prep for an upcoming...

Hello fellow programmers :D

I have a little issue, this is a prep for an upcoming test, I have a wizard named Gorrok and I want him to cast a spell with the getSpell function, and I want it to be a random spell from the array I made called spells, I implemented the math.random method, for some reason it's telling me its 'undefined' is it possible that someone can find the solution and explain it to me! This isn't for marks its just for practice.

let wizardMan = {};

wizardMan.first_name = "Gorrok: ";

spells  = ['abracadabra', 'cadabraabra', 'wooz'];

var randomSpell = [Math.floor(Math.random()*spells.length)];


wizardMan.getSpell = function(){

     

    console.log(this.first_name + '  ' + this.spells);

    return this.first_name + '  ' + this.spells;

}

wizardMan.getSpell();

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Remove the this keyword from spells, spells is global variable and not part of this


wizardMan.getSpell = function(){

console.log(this.first_name + ' ' + spells);

return this.first_name + ' ' + spells;

}

SEE OUTPUT

> wizardMan.getSpell = function() { console.log(this.first_name + + spells); return this.first name + + spells; < f (

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Hello fellow programmers :D I have a little issue, this is a prep for an upcoming...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT