SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    arr=[1,2,3,4,5,6] will be arr = [ [1,2], [3,4], [5,6] ];

    arr=[1,2,3,4,5,6] will be arr = [ [1,2], [3,4], [5,6] ];
    Message box is empty. What can I do? Thanks...
    Code:
     
    <script type="text/javascript">
    
    Array.grupla=function(arr,grup,numberOfEleman){
    var A=[];
    for(var i=0; i<arr.length; i++) {
    for(var k=0; k<grup.length; k++){ 
    for(var n=0; n<numberOfEleman.length; n++) { 
    A[k][n]=dizi[i];
    }
    }
    }
    return A;
    }
    var arr=[1,2,3,4,5,6];
    arr=Array.grupla(arr,3,2);
    alert(arr);
    
    </script>
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The k loop goers until k < grup.length
    What is grup and does it have a length?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Array.prototype.groupsof= function(n){
    	var A= this.slice(0), B=[];
    	while(A.length) {
    		B.push(Array(A.splice(0,n)));
    	}
    	return B;	
    }
    var A=[1,2,3,4,5,6];
    alert(A.groupsof(2).join('\n'));

  4. #4
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    grup.length = 3;
    arr=Array.grupla(arr,3,2);

    arr = [
    [1,2],
    [3,4],
    [5,6]
    ];
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  5. #5
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo
    your code works.
    I want message box displays
    alert(A[1][1]); // returns 4
    But it displays undefined. How can I do?
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    Code:
    Array.prototype.groupsof= function(n){
    	var A= this.slice(0), B=[];
    	while(A.length) {
    		B.push(Array(A.splice(0,n)));
    	}
    	return B;	
    }
    var A=[1,2,3,4,5,6];
    alert(A.groupsof(2).join('\n'));
    Wow !

    Sorry but I don't understand var A= this.slice(0)
    I think you can do

    PHP Code:
    Array.prototype.groupsof= function(n){
        var 
    B=[];
        while(
    this.length) {
            
    B.push([this.splice(0,n)]);
        }
        return 
    B;    
    }
    var 
    A=[1,2,3,4,5,6];
    alert(A.groupsof(3).join('\n')); 
    or not ?

    Bye.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    There are lots of problems with the original code. Numbers don't have a length, so grup.length and numberofElem.length will just want to be grup and numberOfElem respectively. Before A[k][n] can be created, there needs to first exist an A[k], and what on earh is dizi?

    Go with the code from mrhoo instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code is doing what I wanted.
    Code:
     
    <script type="text/javascript">
    
    var arr=[1,2,3,4,5,6];
    var A=[];
    var s=0, m=0;
    for(var i=0; i<arr.length; i+=2){
    A[s]=[];
    for(var n=0; n<2; n++) {
    A[s][n]=arr[m++];
    }
    s++;
    }
    
    alert(A);
    alert(A[1][1]);  // 4
    
    </script>
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  9. #9
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by muazzez View Post
    This code is doing what I wanted.
    Code:
     
    <script type="text/javascript">
    
    var arr=[1,2,3,4,5,6];
    var A=[];
    var s=0, m=0;
    for(var i=0; i<arr.length; i+=2){
    A[s]=[];
    for(var n=0; n<2; n++) {
    A[s][n]=arr[m++];
    }
    s++;
    }
    
    alert(A);
    alert(A[1][1]);  // 4
    
    </script>

    PHP Code:
    Array.prototype.groupsof= function(n){
        var 
    Athis.slice(0), B=[];
        while(
    A.length) {
            
    B.push(Array(A.splice(0,n)));
        }
        return 
    B;    
    }
    var 
    A=[1,2,3,4,5,6]; 
    var 
    tmpA.groupsof(2);
    alert(tmp[0][0][0]);
    alert(tmp[0][0][1]); 

  10. #10
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everybody.
    What a beautiful day!
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  11. #11
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry but I don't understand var A= this.slice(0)
    I wrote it that way so that you would not change the original array, but return a new array with the array groups you specify.

    You can discard the original with A=A.groupsof(2), but every occasionally you may need to refer to the original array.
    whisher wants to alert A[1][1], so he must set A=A.groupsof(2);

    But it is certainly not required!
    Last edited by mrhoo; Apr 8, 2008 at 10:07.

  12. #12
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok

  13. #13
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what exactly are you trying to achieve: should [1..8] be converted to "[[1, 2], [3, 4], [5, 6], [7, 8]]" or "[[[1, 2]], [[3, 4]], [[5, 6]]]". Anyways, this can be probably done simpler:

    Code:
    Array.prototype.group = function(n) {
    	for(var i = 0, t = []; i < this.length; i += n)
    		t.push(this.slice(i, i + n));
    	return t;
    }

  14. #14
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stereofrog,
    Your code converted to [[1, 2], [3, 4], [5, 6]];


    var arr=[1,2,3,4,5,6];
    alert(arr.toSource()); // [1,2,3,4,5,6]
    var s = arr.group(2);
    alert(s.toSource()); // [[1, 2], [3, 4], [5, 6]];
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  15. #15
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this is what you wanted isn't it?

  16. #16
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I use all of them.
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •