0

Need help with this JavaScript question:

Now create three new objects. Each object should have the following properties:

  • type: a string specifying what type of mysticalAnimal this is. Unicorns and dinosaurs and yeti and Loch Ness Monsters and polar bears and red pandas are all viable options!
  • collects: an array with a few things this animal might collect
  • canFly: a boolean (true or false — no strings around it, these are reserved keywords) representing whether this animal can fly or not.

Our small paired programming group tried:

var myArray = [];
myArray.push(myObject);
var chipotle = ['unicorn', 'food', true];

How would we properly address this? What is the correct code?

4
  • I read it, but know what you're looking for exactly. You need post of clearer question, either that or I don't get it. Commented Aug 6, 2015 at 2:15
  • 2
    That's not an object. An object has curly braces, keys, and values. If it is a custom type, use its constructor. Commented Aug 6, 2015 at 2:15
  • 'food' is not an array but a string. Commented Aug 6, 2015 at 2:19
  • I guess the question is how do you create a object with three properties and then add that object into an array correctly. So all the code together. Commented Aug 6, 2015 at 2:53

2 Answers 2

2

Here is an example.

An object looks like this:

var Rudolph = {
   type: 'magic reindeer',
   collects: ['insults','sleigh','toys'],
   canFly: true
 }

The above code creates an Object. Objects are very general, the keys are effectively strings and the values can be a string, a number, or even an arrays or another object.

If you are studying custom classes in Javascript, there might be a constructor function that you are supposed to write, or use.

If you had, or are given, a function

function mysticalAnimal(type, collects, canFly){
    this.type = type;
    this.collects = collects;
    this.canFly = canFly;
}

then you could use

var Rudolph = new mysticalAnimal('magic reindeer', 
                                 ['insults','sleigh','toys'],
                                 true);

to create an object that is an instance of mysticalAnimal.

One advantage of this is that the custom function-based object's origin can be tested with:

Rudolph instanceof mysticalAnimal
---> true
Sign up to request clarification or add additional context in comments.

2 Comments

@Franklinc sounds like another question. Stack Overflow is an attempt to build a library of questions and answers, not answer all of a visitor's questions in one spot. Adding an object to an array is answered here: stackoverflow.com/questions/6254050/…
Thank you again for your assistance on this awhile back. Definitely was just starting. I didn't even know how to select the check mark on Stack Overflow to show the question as answered. Also, my question phrased here was horrid. Thanks again for the time you spent!
0

See Object - MDN

You may create one like this:

var obj = {
  type: 'Unicorns',
  collects: ['stuff1', 'stuff2'],
  canFly: true
}

Comments

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.