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

159
Visualizações
replacing letters in a loop fault

I'm trying to figure out why my output is not correct it kinda works but still has some faults in the loop I can't seem to find why. I know there are better ways to do this but i want to learn what went wrong in this loop.

So first it ask a sentence second question is to replace 2 letters like : a e third question is to replace it with like : o u

I then split the karakters and put the sentence in an array. The loop checks every letter in the array and if it comes across one of the letters it should replace it with the one found.

var input = prompt("geef een zin ?");
var zin = [];
zin = input.split("");
var inn = prompt("welke karakters wil je veranderen?");
var karaktersinn = inn.split("");
var outt = prompt("in welke letters wil je ze veranderen?");
var karatersout = outt.split("");
var output = "";
console.log(input);


for (var i = 0;i < input.length-1;i++){
   if (zin[i] == karaktersinn[0]){
      output += karatersout[0]

   }if (zin[i] == karaktersinn[2]){
      output += karatersout[2]
   }else{
      output += zin[i];
   }
}

console.log(output);
console.log(zin)
console.log(karaktersinn)
console.log((karatersout))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your if conditions are structured like this :

if 

if 

else

This means you have two if else blocks. So regardless of what happens in the first block, the second block will always be checked, and one of the the last conditions will run

You need an if-else. So at one time only one of the three conditions run.

var input = prompt("geef een zin ?");
var zin = [];
zin = input.split("");
var inn = prompt("welke karakters wil je veranderen?");
var karaktersinn = inn.split("");
var outt = prompt("in welke letters wil je ze veranderen?");
var karatersout = outt.split("");
var output = "";
console.log(input);


for (var i = 0;i < input.length;i++){
   if (zin[i] == karaktersinn[0]){
      output += karatersout[0]

   }else if (zin[i] == karaktersinn[2]){
      output += karatersout[2]
   }else{
      output += zin[i];
   }
}

console.log(output);
console.log(zin)
console.log(karaktersinn)
console.log((karatersout))

Also you are running one less iteration of the loop. Fixed that.

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