0

Problem: I'm trying to create a constructor function named Wizard that takes 2 parameters: name and spells, and then create a Wizard object:

"Each particular instance of wizard must have a name property (a string), a spells property which is an array of strings, and a castSpell method capable of returning a random spell in string format."

The object has the following properties: name is "Gorrok" (string), and spells is "abracadabra" and "cadabraabra" (array).

Objective: to invoke the castSpell method to display a random spell like so: Gorrok : abracadabra

Code: I've only written the following code so far, and I'm stuck at this stage!

function Wizard(name, spells){
    this.name = name;
    this.spells = [spells];
    this.castSpell = function(){
        var v = Math.random();
        if (v >= 1)
            document.write(this.name + " :  " + this.spells[0]);
        else
            document.write(this.name + " :  " + this.spells[1]);
    }
}
var w = new Wizard("Gorrok", "abracadabra", "cadabraabra");
w.castSpell();

3 Answers 3

2

So, Math.random() will return a number between 0 and 1, so it'll never be bigger than 1.

Also, you can't convert the remaining arguments to an array the way you have.

Simplest fix:

function Wizard(name, spells){
    this.name = name;
    this.spells = spells; // assume spells is already an array
    this.castSpell = function(){
        var v = Math.random();
        if (v >= 0.5)
            document.write(this.name + " :  " + this.spells[0]);
        else
            document.write(this.name + " :  " + this.spells[1]);
    }
}
var w = new Wizard("Gorrok", ["abracadabra", "cadabraabra"]);
w.castSpell(); 
Sign up to request clarification or add additional context in comments.

Comments

1

I think Paul's answer is correct. Also, for functions not in ctor, use prototype. Prototypes also allow you add member vars that are not defined in constructor.

Here's an example from w3:

function Person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";

1 Comment

It's worth pointing out that using the prototype has an added benefit on memory allocation in most javascript runtime environments. When a function is defined in the constructor, it allocates a new instance of the function to each instance of the object, while using the prototype as this answerer has done will allocate only one copy of the function to all instances of the class.
0

You can also use Math.round() to get 0 or 1 randomly

var v = Math.round( Math.random() );
document.write(this.name + " :  " + this.spells[v]);

2 Comments

This way of shuffling was new to me, I like what you did there. Exactly in which situations would you use Math.round(Math.random())? And can you use other math classes within itself?
Math.random returns a float between 0 and 1, Math.round rounds a float to the nearest integer. You are not limited when you use Math.{someFunction} result. It can be written like var floatBetween0and1 = Math.random(); var number0or1 = Math.round(floatBetween0and1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.