0

I'm working on a program that, before it can do anything else, must be able to convert numbers into other numbers. The program only works with numbers 0 through 11 (representing music notes), so anything greater or less than that must be converted to a number between 0 and 11. But when I run my function, it returns undefined for a lot of numbers.

const TET = 12;
const Notes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

function NoteConverter(note) {
  const doubled = TET * 2;
  const tripled = TET * 3;

  for (index in Notes) {
    if (note == index) {
      return index;
    } else if (note == index + TET || note == index - TET) {
      return index;
    } else if (note == index + doubled || note == index - doubled) {
      return index;
    } else if (note == index + tripled || note == index - tripled) {
      return index;
    }
  }
}

When I run the function using 15 as the number to be converted, I get undefined instead of 3, which it should convert to.

I've been trying to figure out what I'm doing wrong but can't. Any help?

  • Your `Notes` array is only 12 big. Your `for` will not get to 15, so you return `undefined` implicitly at the end of your function. – General Grievance Apr 11 '22 at 16:14
  • 1
    Never use `for...in` with arrays. Use `for...of` or use the `.forEach(...)` iterator function. But also note that arrays have `.find(...)` and `findIndex(...)`, there is no need to write your own for loop here, just find the index and then do what you need to do? – Mike 'Pomax' Kamermans Apr 11 '22 at 16:15
  • 1
    Or in this case, since it's just a sequence of 0-11, just a plain old for loop will do: `for (let i = 0; i < 12; i++)`. – General Grievance Apr 11 '22 at 16:16
  • You could use the [modulus operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder) to do this in one line - `return note % TET;` – James Apr 11 '22 at 16:20

2 Answers2

0

It will concatenate string for index + TET so for example if index is 0 index + TET will be 012 it happens because typeof index is string also it is string because the in keyword also works for objects so it would return key name for object which isn't always a number.

edit: so you either have to go with of notes as suggested in the other answer or make sure that it gets converted to number first ;)

miyav miyav
  • 338
  • 1
  • 5
  • 18
0

Just replace in with of

const TET = 12;
const Notes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

function NoteConverter(note) {
  const doubled = TET * 2;
  const tripled = TET * 3;

  for (index of Notes) {
    if (note == index) {
      return index;
    } else if (note == index + TET || note == index - TET) {
      return index;
    } else if (note == index + doubled || note == index - doubled) {
      return index;
    } else if (note == index + tripled || note == index - tripled) {
      return index;
    }
  }
}

console.log(NoteConverter(15))
R4ncid
  • 6,944
  • 1
  • 4
  • 18