Advertisement
pacho_the_python

Print every N-th Element from an Array

Feb 22nd, 2023
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(list, num) {
  2.     let counter = 0;
  3.     let  current_list = [];
  4.     while (true) {
  5.         if (counter >= list.length) {
  6.             break
  7.         }
  8.         current_list.push(list[counter]);
  9.         counter += num;
  10.     }
  11.     console.log(current_list);
  12. }
  13.  
  14. solve(['5', '20', '31', '4', '20'], 2)
  15. solve(['dsa', 'asd', 'test', 'tset'], 2)
  16. solve(['1', '2', '3', '4', '5'], 6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement