DEV Community

k@k
k@k

Posted on

JavaScript Challenge #4

let m x n array is given below

[ 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20 ];

Expected Output: If you give array [i,j] => i.e 10, it will print the square of numbers for example

     5  6  7
     9  10 11
    13  14 15

Understand the problem and solve it. make your solution on comments..

Discussion (3)

Collapse
kenbellows profile image
Ken Bellows • Edited on

What do you mean by "if I give it 10"? Do you mean we should write a function? What does 10 mean, or is this a cryptic challenge, the kind where the first puzzle is to understand the question itself, like cryptic crosswords?

Also, you said "m x n array", which is usually used to refer to a 2-D array, i.e. an array with m arrays in it, each of length n:

[[1,  2,  3,  4],
 [5,  6,  7,  8],
 [9,  10, 11, 12],
 [13, 14, 15, 16],
 [17, 18, 19, 20]]

But the example array you gave seems to be a 1-D array with m*n elements. Which was your intention?

Collapse
kenbellows profile image
Ken Bellows

Ah, I see, you mean the square of numbers around the given value, in this case 10. I recommend wrapping your example array in a code block with triple backticks (check the formatting help of you don't know what I mean) to make it easier to understand what you mean.

So yeah, to repeat the second half of my question, is the input a 2-D array, or a 1-D array with a pair of dimension variables, e.g. function getSquare(arr, m, n, val) { ... }?

Collapse
kmk profile image
k@k Author

Input is 2D Array