[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

multi dim array

jrough

2/17/2015 10:48:00 PM

I'm trying to get this output from a ;
1-1
1-3
1-5
3-3
3-5
5-5
Would this be a 4 dimensional array? Otherwise does this algorithm make sense? I had a test interview question and maybe I didn't understand the question. I was trying to do it anyway. This gets a syntax error.
var a = [1,3,5];
for (var i=0;i< a.length; i++;){
for( var j=9;j< a.length; j++){
var myarr=[];
push.myarr[i,j] ;
console.log(myarr[i,j]);
};
};
6 Answers

ram

2/17/2015 11:21:00 PM

0

JRough <janis.rough@gmail.com> writes:
>I'm trying to get this output from a ;
>1-1
>1-3
>1-5
>3-3
>3-5
>5-5
>var a = [1,3,5];

"use strict";
var a =[ 1, 3, 5 ];
for( var i = 0; i < a.length; ++i )
for( var j = i; j < a.length; ++j )
document.writeln( a[ i ], '-', a[ j ] );

Christoph M. Becker

2/18/2015 12:48:00 AM

0

JRough wrote:

> I'm trying to get this output from a ;
> 1-1
> 1-3
> 1-5
> 3-3
> 3-5
> 5-5
> Would this be a 4 dimensional array?

Why should that be a four dimensional array? If at all, it might be
some representation of a two dimensional array. Note, however, that
ECMAScript implementations (usually) have only one dimensional arrays.

> Otherwise does this algorithm make sense?

Which algorithm?

> I had a test interview question and maybe I didn't understand the question. I was trying to do it anyway. This gets a syntax error.

Well, you've screwed up. :)

> var a = [1,3,5];
> for (var i=0;i< a.length; i++;){
> for( var j=9;j< a.length; j++){

You're trying to run a loop from 9 to 2 with a step (increment) of 1.
Surely, that'll never execute the loop body.

> var myarr=[];
> push.myarr[i,j] ;

Most likely there is no `push` object, so you can't access one of its
properties (`push.myarr`); probably you confused that with `myarr.push`.
Anyhow, [i,j] is not a valid property accessor, but rather a syntax error.

> console.log(myarr[i,j]);

Same here.

> };
> };

I suggest you learn about the syntax and semantics of ECMAScript, and
make use of one of the many "interactive shells" widely available
nowadays, such as the script consoles of contemporary browsers.

--
Christoph M. Becker


JR

2/18/2015 3:22:00 AM

0

JRough wrote:
> I'm trying to get this output from a ;
> 1-1
> 1-3
> 1-5
> 3-3
> 3-5
> 5-5
> Would this be a 4 dimensional array?

There is no multidimensional array in JavaScript / ECMASCript. You can
create an array of arrays, though. But that's not the case here.

> Otherwise does this algorithm make sense? I had a test interview [...]

> var a = [1,3,5];
> for (var i=0;i< a.length; i++;){
> for( var j=9;j< a.length; j++){
> var myarr=[];
> push.myarr[i,j] ;
> console.log(myarr[i,j]);
> };
> };
>

The nested for-loop might be something like this:

var a = [1, 3, 5];
for (var i = 0, len = a.length; i < len; i++) {
for (var j = i; j < len; j++) {
console.log(a[i] + '-' + a[j]);
}
}

You can use the forEach method [1], although it may be a little less
efficient way than using nested loops as above, depending on the number
of elements.

var a = [1, 3, 5];

a.forEach(function(x, i) {
a.slice(i).forEach(function(y) {
console.log (x + '-' + y);
});
});

[1]
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/f...

--
Joao Rodrigues

Denis McMahon

2/18/2015 2:12:00 PM

0

On Tue, 17 Feb 2015 14:48:14 -0800, JRough wrote:

> I'm trying to get this output from a ;

> 1-1 1-3 1-5 3-3 3-5 5-5

> Would this be a 4 dimensional array?

No

> Otherwise does this algorithm make sense?

Given an 'a' of [1,3,5] as used in your code the output is certainly
attainable with a fairly simple algorithm.

> I had a test interview question and maybe I didn't understand
> the question.

That seems likely. Please state the actual test question. You have not
done so, and given that you may have not understood it, I am unwilling to
attempt to deduce the coding task you were set from your attempted
solution.

--
Denis McMahon, denismfmcmahon@gmail.com

ram

2/18/2015 2:18:00 PM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>JRough <janis.rough@gmail.com> writes:
>>I'm trying to get this output from a ;
>>1-1
>>1-3
>>1-5
>>3-3
>>3-5
>>5-5
>>var a = [1,3,5];
>"use strict";
>var a =[ 1, 3, 5 ];
>for( var i = 0; i < a.length; ++i )
>for( var j = i; j < a.length; ++j )
>document.writeln( a[ i ], '-', a[ j ] );

A shorter script to produce the same output would be,
of course:

"use strict";
var a =[ 1, 3, 5 ];
document.writeln( "1-1\n1-3\n1-5\n3-3\n3-5\n5-5" );

.

Evertjan.

2/18/2015 4:55:00 PM

0

ram@zedat.fu-berlin.de (Stefan Ram) wrote on 18 feb 2015 in
comp.lang.javascript:

> A shorter script to produce the same output would be,
> of course:
>
> "use strict";
> var a =[ 1, 3, 5 ];

Why do you need the above?

> document.writeln( "1-1\n1-3\n1-5\n3-3\n3-5\n5-5" );

Not making new lines on a html page.

Try:

document.write('111315333555'.replace(/(\d)(\d)/g,'$1-$2<br>'));



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)