Double Cola Codewars

by Sascha

JavaScript

function whoIsNext(names, r){
  /* Way to memory and time consuming
  for (let i=1;i<r;i++) {
    let name= names.shift();
    names.push(name, name);
  }
  return names[0];
  */
  let n=0;
  for (n=0;Math.pow(2, n)*5 <= r;n++) {
    
  }
  let rest= r % (Math.pow(2, n-1)*5);
  console.log(n, rest);
}

names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
Test.assertEquals(whoIsNext(names, 2), "Leonard");
Test.assertEquals(whoIsNext(names, 1), "Sheldon");
Test.assertEquals(whoIsNext(names, 10), "Penny");
Test.assertEquals(whoIsNext(names, 11), "Penny");
Test.assertEquals(whoIsNext(names, 12), "Rajesh");
Test.assertEquals(whoIsNext(names, 14), "Howard");