skip to Main Content

I have the array and need to divide 3 student per 3 array. Need to get the following results per cases:

  1. sortStudentsByGroups(students)  =>

    [  [ 'Andrew', 'Ann', 'Bernard' ],  [ 'Cris', 'Josh', 'Mark' ],  [ 'Peter', 'Sam', 'Sandra' ],  'Rest students: Takesi']
    
  2. if to delete one student from the list, will get:

    [
      [ 'Andrew', 'Ann', 'Bernard' ],
      [ 'Cris', 'Josh', 'Mark' ],
      [ 'Peter', 'Sam', 'Sandra' ],
      'Rest students: -'
    ]
    

My CODE:

const students = [
  "Peter",
  "Andrew",
  "Ann",
  "Mark",
  "Josh",
  "Sandra",
  "Cris",
  "Bernard",
  "Takesi",
  "Sandra",
];

function sortStudentsByGroups(arr) {
  arr.sort();
  const arrNew = [];
  const arr_size = 3;

  for (let i = 0; i < arr.length; i += arr_size) {
    let y = arr.slice(i, i + arr_size);

    arrNew.push(y);

    if (y.length < 3) {
      let u = y.join(", ");
      arrNew.push(`Rest students: ${u}`);
    }
  }

  console.log(arrNew);
}

sortStudentsByGroups(students);

For 1 option I have correct result:

[
  [ 'Andrew', 'Ann', 'Bernard' ],
  [ 'Cris', 'Josh', 'Mark' ],
  [ 'Peter', 'Sandra', 'Sandra' ],
  [ 'Takesi' ],
  'Rest students: Takesi'
]

And don’t know how to get result for 2-nd option, exactly add to arr ‘Rest students: -‘

[
  [ 'Andrew', 'Ann', 'Bernard' ],
  [ 'Cris', 'Josh', 'Mark' ],
  [ 'Peter', 'Sam', 'Sandra' ],
  'Rest students: -'
]

I tried to push in array and as the result iterating every time

4

Answers


  1. One possibility would be to distribute normally, then pick the last element and check its size:

    function sortStudentsByGroups(arr, size) {
        const arrNew = [];
    
        for (let i = 0; i < arr.length; i += size) 
            arrNew.push(arr.slice(i, i + size))
        
        let last = arrNew.pop()
    
        if (last.length < size)
            arrNew.push('Rest: ' + last.join())
        else
            arrNew.push(last, 'Rest: -')
    
        return arrNew
    }
    
    console.log(...sortStudentsByGroups('A B C D E F'.split(' '), 3))
    console.log(...sortStudentsByGroups('A B C D E F G'.split(' '), 3))
    console.log(...sortStudentsByGroups('A B C D E F G H'.split(' '), 3))
    Login or Signup to reply.
  2. you can do your function like that:

    function sortStudentsByGroups(arr) {
      arr.sort();
      const arrNew = [];
      const arr_size = 3;
    
      let remainingStudents = [...arr];
    
      for (let i = 0; i < arr.length; i += arr_size) {
        let y = remainingStudents.slice(0, arr_size);
    
        arrNew.push(y);
    
        if (y.length < 3) {
          let u = y.join(", ");
          arrNew.push(`Rest students: -`);
        }
    
        remainingStudents = remainingStudents.slice(arr_size);
      }
    
      console.log(arrNew);
    }
    
    Login or Signup to reply.
  3. You have handled the case, when you have "y" value <3 i.e, if y is 3, then you have appended right? so if the no. of people are multiple of 3 only, your 2nd test case fails.

    so, you can add else condition here,

    const students = [
      "Peter",
      "Andrew",
      "Ann",
      "Mark",
      "Josh",
      "Sandra",
      "Cris",
      "Bernard",
      "Takesi",
      "Sandra",
    ];
    
    function sortStudentsByGroups(arr) {
      arr.sort();
      const arrNew = [];
      const arr_size = 3;
    
      for (let i = 0; i < arr.length; i += arr_size) {
        let y = arr.slice(i, i + arr_size);
    
        arrNew.push(y);
    
        if (y.length < 3) {
          let u = y.join(", ");
          arrNew.push(`Rest students: ${u}`);
        }
        if (i+arr_size >= arr.length){
        arrNew.push("Rest students: -");
        }
        
    
      }
    
      console.log(arrNew);
    }
    Login or Signup to reply.
  4. Move your rest students logic outside the loop and use

    arr.slice(-arr.length % arr_size || arr.length)
    

    to get the remaining students and join them with , .
    If the string is empty that means no students left, so add || '-' to display the dash.

    Btw you mutate the original array with sort(). If that’s not desired, just copy the array: arr = arr.slice().sort().

    const students = [
      "Peter",
      "Andrew",
      "Ann",
      "Mark",
      "Josh",
      "Sandra",
      "Cris",
      "Bernard",
      "Takesi",
      "Sandra",
    ];
    
    const students2 = [
      "Peter",
      "Andrew",
      "Ann",
      "Mark",
      "Josh",
      "Sandra",
      "Cris",
      "Bernard",
      "Sandra",
    ];
    
    function sortStudentsByGroups(arr) {
      arr.sort();
      const arrNew = [];
      const arr_size = 3;
    
      for (let i = 0; i < arr.length; i += arr_size) {
        let y = arr.slice(i, i + arr_size);
        arrNew.push(y);
      }
      
      arrNew.push(`Rest students: ${arr.slice(-arr.length % arr_size || arr.length).join(', ') || '-'}`);
    
      return arrNew;
    }
    
    sortStudentsByGroups(students).map(item => console.log(JSON.stringify(item)));
    sortStudentsByGroups(students2).map(item => console.log(JSON.stringify(item)));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search