File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } students
3
+ * @param {number[] } sandwiches
4
+ * @return {number }
5
+ */
6
+ var countStudents = function ( students , sandwiches ) {
7
+ let movement = 0 ; // initialize movement to be zero
8
+
9
+ while ( sandwiches . length > 0 ) { // while length of sandwiches is greater than zero
10
+ if ( students [ 0 ] == sandwiches [ 0 ] ) { // if first element of students and sandwiches both are same
11
+ students . shift ( ) ; // reomve first element of students using shift()
12
+ sandwiches . shift ( ) ; // remove first element of sandwiches using shift()
13
+ movement = 0 ; // make movement to be zero
14
+ } else { // else
15
+ let add = students . shift ( ) ; // initialize add to be first element of students
16
+ students . push ( add ) ; // push add to array students
17
+ movement ++ ; // increment movement
18
+ if ( movement == students . length ) { // if movement is equal to length of students then break the loop
19
+ break ;
20
+ }
21
+ }
22
+ }
23
+
24
+ return students . length ; // return length of array students
25
+ } ;
You can’t perform that action at this time.
0 commit comments