-4

I have made array and all but i only need function that clears every second item from list and doing that job until there is only 1 item left for example i need something to do in array from 1-10 including 1 and 10 the result need to be 5?

Any sugestions it is similar like this but only need it for javascript not python How to delete elements of a circular list until there is only one element left using python?

I am using this inside html body tag

var num = prompt("type num"); 
var array = []; 
for (i = 1; i <= num; i++) {  
  array.push(i);   
}  
document.write(array + "<br>");

I tried this so far but this does not finish jobs great

    while (i--) {
  (i + 1) % 2 === 0 && array.splice(i, 1)
}

It does only first time deleting and leave array 1 3 5 7 9 i need it to be only 5 in this case because prompt is 10 in my case

jhpratt
  • 6,841
  • 16
  • 40
  • 50
  • 1
    Post some examples and the code you've tried. Before downvoters arrive. – ibrahim mahrir Nov 23 '17 at 21:49
  • using this inside html body tag – E1337K MOVIES Nov 23 '17 at 21:50
  • i have tried while (i--) { (i + 1) % 2 === 0 && array.splice(i, 1) } but it only does job one time and left every second including one 1 3 5 7 9 and does not repeat what to do? – E1337K MOVIES Nov 23 '17 at 21:53
  • 3
    Please UPDATE THE QUESTION instead of pasting unformatted code into comments. Your document.write looks VERY weird with that illegal `` tag – mplungjan Nov 23 '17 at 21:54
  • Sorry updated check now my false
    tag does not make problem for me feel free to correct it if im wrong
    – E1337K MOVIES Nov 23 '17 at 21:59
  • If I start with `[1,2,3,4,5,6,7,8,9,10]`, and I remove every second item, I get `[1,3,5,7,9]`. If I do that again, I get `[1, 5, 9]`, again = `[1,9]`, again = `[1]`. So either your expected answer is wrong or you're not removing every second item over and over again. edit - hmm perhaps the clue is in the link, `circular` - is that what you want? – James Nov 23 '17 at 22:10
  • After fist time you get 1 3 5 7 9 then 1 removes 3 5 removes 7 9 removes 1 and you are left wth 5 and 9 so 5 is now first number that is not removed and it remove 9 better explanation now? – E1337K MOVIES Nov 23 '17 at 22:14

2 Answers2

0
while(array.length > 1) {
    array = array.filter((_, index) => index % 2 === 0);
}

Basically, get rid of every second index as long as the length is greater than 1. The callback for filter allows value as the first parameter and index as the second, per MDN.

jhpratt
  • 6,841
  • 16
  • 40
  • 50
0

The circular part makes it pretty tricky. Your loop condition should be on array length > 1, and within the loop you have to manually mess with the counter once it exceeds the length of the array - 2. If it's equal to arr.length, you want to skip the first element of the array the next time around, otherwise begin with the first element. Sorry I'm not explaining it better, here is the code.

var arr = [1,2,3,4,5,6,7,8,9,10];
var i = 1;

while (arr.length > 1) {
  console.log("removing " + arr[i]);
  arr.splice(i, 1);
 
  var left = arr.length - i;
  if (left == 0)
    i = 1;
  else if (left == 1)
    i = 0;
  else
    i++;
}
  
console.log("result " + arr[0]);

Edit - This is almost exactly The Josephus Problem or see the episode from Numberphile on Youtube

There is a short recursive way to solve it. The parameter n is the largest number (similar to an array of n numbers from 1 to n), and k being the number of positions to skip at a time, (every other being 2).

var josephus = (n, k) => {
  if (n == 1) return 1;
  return (josephus(n - 1, k) + k-1) % n + 1;
};
console.log(josephus(10, 2));
James
  • 20,957
  • 5
  • 26
  • 41