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

154
Visualizações
Getting undefined but i would like to use else statement inside for loop and display 'not exist' instead. please help me to fix

How can i use else statement in the following, would like to replace undefined to 'not exist', tried but not working. program: Check a given string contains 2 to 4 numbers of a specified character

function check_char(x, y) {
  for (var j = 0; j < y.length; j++) {
    var d = y[j];
  }
  for (var i = 1; i <= 3; i++) {
    data = x[i]
    console.log(data)
    if (data == d) {
      return d + " exist";
    }
  }
}

document.write(check_char("Python", "y") + "<br>");
document.write(check_char("JavaScript", "a") + "<br>");
document.write(check_char("Console", "o") + "<br>");
document.write(check_char("Console", "C") + "<br>");
document.write(check_char("Console", "e") + "<br>");
document.write(check_char("JavaScript", "S") + "<br>");

**output:** 
y exist
a exist
o exist
undefined
undefined
undefined
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Strictly speaking you don't need the else. Keep a count of the number of times the character appears in the string. If the count >= 2 and <= 4 return "exists" otherwise just return "does not exist".

You should avoid using document.write. This example has an extra function that creates some HTML using the string returned from the checkChar function, and then uses insertAdjacentHML to add it to the page.

function checkChar(str, char) {
  
  // Initialise the count 
  let count = 0;
  
  // Loop over the string - if the
  // letter in the iteration is the same as
  // the character increment the count
  for (const letter of str) {
    if (letter === char) ++count;
  }
  
  // If the count is within bounds return "exists"
  if (count >=2 && count <=4) {
    return `<span class="red">${char}</span> exists`;
  }
  
  // Otherwise return "does not exist"
  return `<span class="red">${char}</span> does not exist`;

}

function write(str) {

  // Create an HTML paragraph string with the string
  // returned from `checkChar`, and then add that
  // to the page
  const para = `<p>${str}</p>`;
  document.body.insertAdjacentHTML('beforeend', para);
}

write(checkChar('Python', 'y'));
write(checkChar('JavaScript', 'a'));
write(checkChar('Console', 'o'));
write(checkChar('Console', 'C'));
write(checkChar('Console', 'e'));
write(checkChar('JavaScript', 'S'));
.red { color: red; }

about 4 years ago · Santiago Trujillo Relatório

0

try this,

function check_char(x, y) {
  for (var j = 0; j < y.length; j++) {
    var d = y[j];
  }
  for (var i = 1; i <= 3; i++) {
    data = x[i]
    console.log(data)
    if (data == d) {
      return d + " exist";
    }
  }
  return 'not exist';
}
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