0

I am working on the below JS and it won't show an output when loaded.

<html>
    <head>
    </head>  

    <body>

        <script type="text/javascript">

            var MAX = 3;

            function structCars() {
                this.make;
                this.model;
                this.color;
            }

            var cars = new Array();

            var makes = new Array (
                "Toyota",
                "Ford",
                "Chevrolet"
                );

            var models = new Array (
                "Camrey",
                "Mustang",
                "Nova"
                );

            var colors = new Array (
                "Blue",
                "Red",
                "Yellow"
                );

            for(var i = 0; i < MAX; i++) {
                var temp = new structCars();
                temp.make = makes[i];
                temp.model = models[i];
                temp.color = colors[i];
                cars.push(temp);
                }

            for(i = 0; i < cars.length(); i++) {
                document.write(i + " " + cars[i].make + ", " + cars[i].model + ", " + cars[i].color + ".<br>");
                }

        </script>

    </body>
</html>  

The output should be something like:

0 Toyota, Camry, Blue

1 Ford, Mustang, Red

2 Chevrolet, Nova, Yellow

But nothing is coming up. This could be something simple, or I am doing this totally wrong. I am starting to learn JavaScript, and this is driving me crazy trying to figure this out..

This is for an online coding assignment and I don't want someone to do my work for me. I have asked my professor, but he is only proficient in C#/Java and not JS.... thanks prof..

Thank you for any and all support.

John

3 Answers 3

1

Array#length works without parenthesis.

for(i = 0; i < cars.length; i++) {
//                       ^^^

For the rest, you could use a constructor with values.

function StructCars(make, model, color) {
    this.make = make;
    this.model = model;
    this.color = color;
}

var MAX = 3,
    makes = ["Toyota", "Ford", "Chevrolet"],
    models = ["Camrey", "Mustang", "Nova"],
    colors = ["Blue", "Red", "Yellow"],
    cars = [],
    i;

for (var i = 0; i < MAX; i++) {
    cars.push(new StructCars(makes[i], models[i], colors[i]));
}

cars.forEach(function (car, i) {
    console.log(i + " " + car.make + ", " + car.model + ", " + car.color);
});

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! I removed the parenthesis and it worked. I haven't tried using a constructor with values as of yet, but I will, looks much more simplified.
0

Change length() as function to cars.length

for(i = 0; i < cars.length; i++) {
                document.write(i + " " + cars[i].make + ", " + cars[i].model + ", " + cars[i].color + ".<br>");
                }

Comments

0

You can use map() method too.

Example :

var makes = new Array (
  "Toyota",
  "Ford",
  "Chevrolet"
);

var models = new Array (
  "Camrey",
  "Mustang",
  "Nova"
);

var colors = new Array (
  "Blue",
  "Red",
  "Yellow"
);

var cars = makes.map(function(value,key){
  var car = {};
  car.make=value;
  car.model=models[key];
  car.color=colors[key];
  return car;
});

console.log(cars);

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.