re: Daily Challenge #69 - Going to the Cinema VIEW POST

FULL DISCUSSION
 

"first" lmao

const movie = (card, ticket, part) => {
  let i = 0
  let a = 0
  let b = card
  do {
    a += ticket
    b += ticket * part ** i
    i++
  } while (Math.ceil(b) >= a)
  return i - 1
}

an "optimization" (no power)

const movie = (card, ticket, part) => {
  let i = 0
  let a = 0
  let b = card
  let cheap = ticket
  while (true) {
    a += ticket
    b += cheap
    cheap *= part
    if (Math.ceil(b) < a) return i
    i++
  }
}

"optimization II" (no Math.ceil, comparison with 0 instead of 1)

const movie = (card, ticket, part) => {
  let i = 0
  let dif = -card - 1
  let cheap = ticket
  while (true) {
    dif += ticket - cheap
    cheap *= part
    if (dif > 0) return i
    i++
  }
}

idiocy.

const movie = (card, ticket, part) => {
  let i = 1
  let dif = -card - 1
  let cheap = ticket
  while ((dif += ticket - (cheap *= part)) <= 0) i++
  return i
}
 
code of conduct - report abuse