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

205
Visualizações
Is there a way to get corresponding index value in a 2nd array using the 1st array as a reference in Javascript?

I am very new to algorithm and programming in general. I may require more explanation than an average fellow. Apologies in advance.

My aim is to Create a function that returns a number, based on the string provided. I have been told that this can be done using loops. I believe my errors lies in the if statement. However, cant seem to figure it out. I would be glad if somebody could help.

function word(s) {
  let str = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"];
  let int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

  for (let j = 0; j < int.length; j++) {
    for (let i = 0; i < str.length; i++) {
      if (s === str[i]) {
        return int[j]
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Essentially what you're looking to do is get the index of the input s in your str array, and return the item at the same index in your int array. You could accomplish this with just the one loop:

function word(s) {
    let str = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"];
    let int = [1,2,3,4,5,6,7,8,9,0];
  
    for (let j = 0; j < int.length; j++){
        if (s === str[j]){
         return int[j]
        }
    }
}

Of course, this is reliant upon the order of values matching up in your str and int arrays.

about 4 years ago · Juan Pablo Isaza Relatório

0

Working Demo :

function getStringNumber(word) {
    let str = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"];
  let int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  
  for (let i = 0; i < str.length; i++) {
    if (word === str[i]) return int[i];
  }
}
  
console.log(getStringNumber('nine') ?? 'No word found!');
  
  

about 4 years ago · Juan Pablo Isaza Relatório

0

Actually, You dont need loop also. You can rearrange the strings and return the index based on the arrangement of the word in array.

const str = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];

function word(s) {
  return str.indexOf(s);
}
console.log(word("five"))

// In case you dont want to change order


function word1(s) {
  const str = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"];
  const index = str.indexOf(s);
  return index < 9 ? index+1 : 0
}

console.log(word1("five"))
console.log(word1("zero"))

// Other way get index ussing findIndex

function word2(s) {
  return str.findIndex(item => item === s);
}
console.log(word2("five"))

// Other using single loop

function word3(s) {
  for (let i in str) {
    if (str[i] === s) return i;
  }
  // Incase not found
  return -1;
}
console.log(word3("five"))

about 4 years ago · Juan Pablo Isaza 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