Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Obteniendo indefinido, pero me gustaría usar la declaración else dentro del bucle y mostrar 'no existe' en su lugar. por favor ayúdame a arreglar

¿Cómo puedo usar la declaración else en lo siguiente? Me gustaría reemplazar indefinido por 'no existe', lo intenté pero no funcionó. programa: compruebe que una cadena dada contiene de 2 a 4 números de un carácter específico

 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 answers
Answer question

0

Estrictamente hablando, no necesitas el else . Lleve la cuenta del número de veces que aparece el carácter en la cadena. Si el recuento >= 2 y <= 4 devuelven "existe", de lo contrario simplemente devuelven "no existe".

Debe evitar usar document.write . Este ejemplo tiene una función adicional que crea algo de HTML usando la cadena devuelta de la función checkChar y luego usa insertAdjacentHML para agregarlo a la página.

 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 Report

0

prueba esto,

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!