0

If you declare an array, you have to assign its elements (or at least acknowledge that it is an array) while declaring it, i.e., var myArray = [1, 2, 3];.

I'm curious as to how one might be able to implement that in an class constructor, for example:

function Matrix(MultiDimensionalArray) {
    this.array = MultiDimensionalArray;
}
var myMatrix = new Matrix() [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

That's the closest thing I could think of to implement this, but I know that it's incorrect. What is the right way to go about this?

3
  • 1
    You are missing commas(,) after every item. Second, your nested array should be passed between parenthesis. Also, functions are declared as function functionMName(argumentList) { function body} Commented Nov 30, 2017 at 7:23
  • Put the array inside your parenthesis. Commented Nov 30, 2017 at 7:24
  • 2
    var myMatrix = new Matrix([[1,2,3],[4,5,6],[7,8,9]]) Commented Nov 30, 2017 at 7:26

3 Answers 3

4

Your constructor call would be like this:

 var myMatrix = new Matrix([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]) 
Sign up to request clarification or add additional context in comments.

1 Comment

It is not a constructor but a constructor call.
0

There are many syntactical mistakes in your code.

  1. Function declaration

You can declare functions like:

function functionName(argList) {}

or

var functionName = function(argList){}
  1. Passing arguments

You have to put arguments within parenthesis. So it should be Matrix([...])

  1. Array declaration

Items in an array are separated using commas. You will have to do [ [], [], [] ].

  1. variable names

In the following code:

function Matrix = (MultiDimensionalArray) {
    this.array = multiDimensionalArray;

variable accepted and variable assigned are different. You will have to use the same values.

Sample Code:

function Matrix(multiDimensionalArray) {
    this.array = multiDimensionalArray;
}
var myMatrix = new Matrix([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]);

console.log(myMatrix.array)

Comments

-1
var myMatrix = new Matrix([[1,2,3],[4,5,6],[7,8,9]]);

5 Comments

This should have been a comment. You are also missing explanation.
I have also commented with an explanation @Rajesh. Please check my comment on his question. Infact I answered after commenting.
Also, the answer above me was also the same. I really don't understand this bias.
@VinodBhavnani I voted your comment as it was correct. But there is a difference between answer and a comment. Also about bias, no one here is anyone's enemy. Your answer looks more like a comment and will attract unwanted voted.
"Put the array inside your parenthesis." is a comment. A piece of code showing how to do it, is an answer. I think I am pretty sure I know what a comment and an answer is. Anyways, if you go around downvoting people because you think something is a comment, while it is an answer, you are actually misinterpreted. A piece of code showing how to do it, cannot be a comment. Period!

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.