DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Number of Students Unable to Eat Lunch

var countStudents = function (students, sandwiches) {
  let count = 0;
  let availableStudent = students;
  let availableSandwhich = sandwiches;

  while (availableStudent.length !== count) {
    const nextStudent = availableStudent.shift();
    const nextSandwich = availableSandwhich.shift();
    if (nextStudent === nextSandwich) {
      count = 0;
    } else {
      count++;
      availableSandwhich.unshift(nextSandwich);
      availableStudent.push(nextStudent);
    }
  }

  return count;
};

Enter fullscreen mode Exit fullscreen mode
var countStudents = (students, sandwiches) => {
    while (students.length) {
        if (students[0] === sandwiches[0]) {
            students.shift();
            sandwiches.shift();
        }
        else if (!students.includes(sandwiches[0]))
            return students.length;
        else students.push(students.shift());
    }
    return 0;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)