Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Number Conversion, Getting Undefined

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?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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 ;)

about 4 years ago · Santiago Trujillo Relatório

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))

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda