Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #26863 > unrolled thread
| Started by | JRough <janis.rough@gmail.com> |
|---|---|
| First post | 2015-02-17 14:48 -0800 |
| Last post | 2015-02-18 17:55 +0100 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.javascript
multi dim array JRough <janis.rough@gmail.com> - 2015-02-17 14:48 -0800
Re: multi dim array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-02-18 01:47 +0100
Re: multi dim array Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-18 01:22 -0200
Re: multi dim array Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-18 14:12 +0000
Re: multi dim array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-02-18 17:55 +0100
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2015-02-17 14:48 -0800 |
| Subject | multi dim array |
| Message-ID | <1bf955cc-1954-4144-a78c-930c8c3d5e5e@googlegroups.com> |
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]);
};
};
[toc] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2015-02-18 01:47 +0100 |
| Message-ID | <mc0nfq$ld8$1@solani.org> |
| In reply to | #26863 |
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
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2015-02-18 01:22 -0200 |
| Message-ID | <mc10gu$jr7$1@speranza.aioe.org> |
| In reply to | #26863 |
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/forEach>
--
Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-02-18 14:12 +0000 |
| Message-ID | <mc26k3$2iq$3@dont-email.me> |
| In reply to | #26863 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2015-02-18 17:55 +0100 |
| Message-ID | <XnsA445B64E843B0eejj99@194.109.133.133> |
| In reply to | #26863 |
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)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web