News for hackers. Stuff that matters.
Re:Teaching the Unfortunate Parts by ghayes on Monday December 28, 2020 @08:19PM (#25565505)
FWIW, I fully support the notation `arr[-0]` as a solution to this problem.
Re:Teaching the Unfortunate Parts by ufo on Monday December 28, 2020 @09:23PM (#25566003)
Even better: just make all arrays one-based instead of zero-based ;)
Re:Teaching the Unfortunate Parts by MauranKilom on Monday December 28, 2020 @08:28PM (#25565587)
So then what should this code produce?

    i = -0
    return arr[i]
Re:Teaching the Unfortunate Parts by thaumasiotes on Tuesday December 29, 2020 @01:36AM (#25567348)
So then what should this code produce?

    i = -0
    return arr[i]
That code would (assuming an integer value; the question is interesting without reference to the implementation of JavaScript) return arr[0], the first element. Don't think of arr[-1] as indexing the array with a negative number that might have been a positive number. Think of array access from the front, arr[ ], and array access from the back, arr[- ], as separate operations with separate syntax, each of which can accept only nonnegative numbers. arr[i] is of the first type, so you get the first element counted from the front.
Re:Teaching the Unfortunate Parts by ufo on Monday December 28, 2020 @09:26PM (#25566027)
Actually, I think that might work in Javascript! All the numbers are IEE754 floating point numbers and there are separate values for positive and negative zero.
Re:Teaching the Unfortunate Parts by disk0 on Monday December 28, 2020 @11:28PM (#25566721)
iirc using numeric literal values there's a coercion step—a console example:

  let arr = new Array(10);
  // 1e1 => 10
  arr[1e1] = 1;
  console.log(`arr[10]  === 1 => ${arr[10]  === 1}`)
  console.log(`arr[1e1] === 1 => ${arr[1e1] === 1}`)

So -0 becomes 0:

  arr[0] = 2;
  console.log(`arr[0]   === 2 => ${arr[0]  === 2}`);
  console.log(`arr[-0]  === 2 => ${arr[-0] === 2}`);
  arr[-0] = 3;
  console.log(`arr[0]   === 3 => ${arr[0]  === 3}`);
  console.log(`arr[-0]  === 3 => ${arr[-0] === 3}`);