1

I have a random number generator and a pick of one of the numbers inside. I'm trying to get the position of the number, So I used index.of() but it always shows '-1'. I thought this would be the most direct way of finding the location of a particular number within an array. Am I wrong?

const shuffle = arr => {
  let a = arr.slice(0); // take a copy
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
};
var arr = [];
var UserNumber = 10;
var BallNumber = 4;
var RandomNumber = Math.floor(Math.random() * UserNumber) + 1;
while (arr.length < BallNumber) {
  var r = Math.floor(Math.random() * UserNumber) + 1;
  if (arr.indexOf(r) === -1) {
    arr.push(r);
  }
  console.log(r);
}
  var selected = shuffle(arr).slice(0, 1);
document.write("<p> The random Number to choose is " + selected + "</p>");
document.write("<p> The Random Numbers are " + arr + "</p>");
document.write("<p> The position is " + arr.indexOf(selected) + "</p>");
RobG
  • 124,520
  • 28
  • 153
  • 188
PlanB
  • 15
  • 6
  • `selected` is an array that you just created using `.slice`. It never existed before, so of course it’s not included in the list. If you want to locate a single _number_, get _a single element:_ `shuffle(arr)[0]`. Please try using the [debugging capabilities](https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) of your browser. `document.write` is [not recommended](https://stackoverflow.com/q/802854/4642212) for debugging or DOM manipulation. Use the [browser console (dev tools)](https://webmasters.stackexchange.com/q/8525) (hit `F12`) and `console.log` instead. – Sebastian Simon Apr 10 '20 at 04:02

2 Answers2

0

slice returns an array, so you'll have to grab the first element, something like:

const shuffle = (arr) => {
  let a = arr.slice(0); // take a copy
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
};
var arr = [];
var UserNumber = 10;
var BallNumber = 4;
var RandomNumber = Math.floor(Math.random() * UserNumber) + 1;
while (arr.length < BallNumber) {
  var r = Math.floor(Math.random() * UserNumber) + 1;
  if (arr.indexOf(r) === -1) {
    arr.push(r);
  }
  console.log(r);
}
var selected = shuffle(arr).slice(0, 1);
document.write("<p> The random Number to choose is " + selected + "</p>");
document.write("<p> The Random Numbers are " + arr + "</p>");
document.write("<p> The position is " + arr.indexOf(selected[0]) + "</p>");
Luís Ramalho
  • 9,209
  • 4
  • 45
  • 63
0

Array.prototype.slice returns an array:

var selected = shuffle(arr).slice(0, 1)

Here, selected is now an array with a single element. But objects are never === to anything besides themselves, and since indexOf uses === to determine indicies, it'll always return -1.

Extract the first value from the shuffled array instead:

var selected = shuffle(arr)[0]

const shuffle = arr => {
  let a = arr.slice(0); // take a copy
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
};
var arr = [];
var UserNumber = 10;
var BallNumber = 4;
var RandomNumber = Math.floor(Math.random() * UserNumber) + 1;
while (arr.length < BallNumber) {
  var r = Math.floor(Math.random() * UserNumber) + 1;
  if (arr.indexOf(r) === -1) {
    arr.push(r);
  }
}
var selected = shuffle(arr).slice(0, 1)[0];
document.write("<p> The random Number to choose is " + selected + "</p>");
document.write("<p> The Random Numbers are " + arr + "</p>");
document.write("<p> The position is " + arr.indexOf(selected) + "</p>");
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
  • 1
    [*indexOf*](http://ecma-international.org/ecma-262/10.0/#sec-array.prototype.indexof) uses the [*Strict Equality Comparison* algorithm](http://ecma-international.org/ecma-262/10.0/#sec-strict-equality-comparison) so pretty much identical to `===`. ;-) – RobG Apr 10 '20 at 04:52
  • Oh, you're right, I had thought it was using SameValueZero like `.includes`, but it's actually `===` – CertainPerformance Apr 10 '20 at 04:54
  • Thanks to everyone who contributed. It really helps me with the breakdown. I just added '+1' to the "position" in order to get it to line up with the number graphics and audio I'm going to be using. – PlanB Apr 10 '20 at 06:10