js-mentorship-razvan / javascript

Javascript study notes
GNU General Public License v3.0
22 stars 2 forks source link

Find all occurrences of an element in an array #457

Closed RazvanBugoi closed 4 years ago

RazvanBugoi commented 4 years ago

https://www.codewars.com/kata/59a9919107157a45220000e1/train/javascript

RazvanBugoi commented 4 years ago
const findAll = (array, n) => {
  let indices = [];
  let idx = array.indexOf(n);
  if (!array.includes(n)) {
    return indices;
} else {
    while(idx != -1) {
      indices.push(idx);
      idx = array.indexOf(n, idx + 1);
}
  return indices;
}
}
RazvanBugoi commented 4 years ago

Can we go over this solution together and explain me what's happening there ? I have solved this kata following an example from dev.docs but I am not sure I understand entirely what's going on there.