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

226
Views
¿Cómo solucionar este error en Javascript que me sigue saliendo?

Resolví el problema. Los casos de prueba seguían fallando probablemente porque había que implementar un bloque try catch. Me olvidé por completo de eso. Los casos de prueba funcionaron cuando agregué el bloque try catch. Gracias por todas las sugerencias. Declaración del problema: necesito diseñar un formulario html simple que tome un límite como entrada y muestre el primer número dado de la serie de Fibonacci. por ej. si se da 5 como entrada, muestra: 0 1 1 2 3 si se da 8 como entrada, muestra: 0 1 1 2 3 5 8 13 Pero sigo recibiendo este error:

 testFiboForNonZeroPositiveInput: Check for the logic and check if the correct output is displayed in div with id 'result' testFiboForZeroInput: Check for the logic and check if the correct output is displayed in div with id 'result'

CASO DE PRUEBA FALLIDO

Aquí está mi código:

 function getFibonacci(){ var fib=document.getElementById("fibo").value; var text; var arr=[]; if (fib.length===0){ text="Please, specify a number."; document.getElementById("result").innerHTML = text; } else if (fib<0){ text="Please, specify a positive number."; document.getElementById("result").innerHTML = text; } else{ var n1 = 0, n2 = 1, nextTerm, i; for (i = 1; i <= fib; i++) { arr.push(n1); nextTerm = n1 + n2; n1 = n2; n2 = nextTerm; } var newStr = arr.join(' ').trim() document.getElementById("result").innerHTML=newStr; } return false; }
 <!DOCTYPE html> <html lang="en"> <head> <title>Fibonacci Series</title> <script src="script.js"></script> </head> <body> <form onsubmit=" return getFibonacci()"> <label for="Enter the number to get a fibonacci">Enter the number to get a fibonacci</label> <input type="number" id="fibo" name="fibo"><br> <input type="submit" value="Get Fibonacci Numbers" id="fibobtn"> <div id="result"></div> </form> </body> </html>

Todo parece funcionar bien y obtengo la serie de Fibonacci y los demás mensajes según sea necesario, pero mis casos de prueba fallan debido a este error. Por favor, dígame qué hacer para solucionar este problema.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Hice estos cambios en el código y los casos de prueba funcionaron: (Se agregó un bloque try catch requerido)

 function getFibonacci(){ try{ var fib=document.getElementById("fibo").value; var text; var arr=[]; if (fib.length===0){ text="Please, specify a number."; document.getElementById("result").innerHTML = text; } else if (fib<0){ text="Please, specify a positive number."; document.getElementById("result").innerHTML = text; } else if(fib>0){ var num1=0; var num2=1; var nextterm; var i=0; for (i = 0; i < fib; i++) { arr.push(num1); nextterm=num1+num2; num1=num2; num2=nextterm; } var newStr = arr.join(' '); document.getElementById("result").innerHTML=newStr; }else { text="0"; document.getElementById("result").innerHTML = text; } } catch(err){ document.getElementById("result").innerHTML=err; } return false; }
about 4 years ago · Juan Pablo Isaza Report

0

Un atributo for en la label debe hacer referencia a una identificación.

 <input type="number" id="fibo" name="fibo"> <label for="fibo">Enter the number to get a fibonacci</label>

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