0

Do anyone understand why console.log(posArr) is printing [1,2,3] and not [0,1,2]. Im trying to push to nOfPos all the index positions of my array :)

function combinations(x) {
  let arr = x.toString().split('');
  console.log(arr)

  let nOfPos = [];
  let posArr = arr.map(x => nOfPos.push(arr.indexOf(arr[x])));
  let mult = posArr.reduce((acum, item) => acum * item);

  console.log(posArr);
  console.log(mult);
}

combinations(123)
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ramiro
  • 11
  • 2
  • Please see [ask], then revise your post title to ask a clear, specific question. Don't ask about what we know. :) – isherwood Sep 29 '22 at 15:24
  • 6
    Array.push returns the new length of the array, so posArr is an array of "new lengths". – James Sep 29 '22 at 15:24
  • 1
    i dont think arr.map(... push) is doing what you expect. .push will return the new length of the array, which is 1 indexed. not 0 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push – Matt Pengelly Sep 29 '22 at 15:25
  • Did you mean `.indexOf(x)`? I expect `x` is an element of `arr` , not an _index_ of an element in `arr`. compare `.map((x, indexOfX) => ...)` – Wyck Sep 29 '22 at 15:34
  • Why is this method called "combinations"? Right now it looks like it's trying to compute the product of the indices of the first occurrence of each element in the array, which includes 0 because the first occurrence of the first element will always be at index 0, which means the answer will always be zero. Surely that's not what you intended. Or is it? What are you doing? – Wyck Sep 29 '22 at 15:44
  • `str.split('')` is a bad habit because it's not Unicode friendly. `[...str]` is better. [See why](https://stackoverflow.com/questions/4547609/how-to-get-character-array-from-a-string). – Wyck Sep 29 '22 at 16:00

2 Answers2

2

The problem is due to two errors in your code. Firstly push() returns the new length of the array, so that's why the mapped output contains the incremental 1,2,3....

Secondly, you need to search for x within arr, so you only need to pass x to indexOf().

With those issues corrected the code works:

function combinations(x) {
  let arr = x.toString().split('');
  let posArr = arr.map(x => arr.indexOf(x));
  let mult = posArr.reduce((acum, item) => acum * item);

  console.log(posArr);
  console.log(mult);
}

combinations(123)

That being said, the purpose of posArr is a little redundant; it will always be an array of incremental values, up to the length of the string -1.

In addition, mult is redundant too as it will always be 0, as you are multiplying by 0.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

You could use a 'for in' loop to get the array indexes.

let posArr = [];
for (ix in arr) {
    posArr.push(ix)
}
Jon Wilson
  • 726
  • 1
  • 8
  • 23
  • That could be just `posArr = [...arr.keys()]` But this is not the same as what would result from `.indexOf` if there are duplicates e.g. `combinations (4455)` – Wyck Sep 29 '22 at 15:50