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

155
Views
tratando de convertir mi código de trabajo en una función

Estoy tratando de convertir este código de trabajo que tengo en una función para un curso que estoy tomando.

 <!DOCTYPE html> <html> <body> <script> var entry; var letterGrade; var total = 0; // you forgot to set the start value for total var entryCount = 0; // also for entry count you must initialize first. // add the error string here so we dont have to duplicate it in the code. var errorString = "Entry must by a valid number from 0 through 100\nOr enter 999 to end entries"; do { // get something from user entry = prompt("Enter number grade from 0 through 100\nOr enter 999 to end entries", 999); // the user may also enter something that is not a number if(isNaN(entry)){ // check if it is not a number alert(errorString); //show error if not a number }else{ // you now know you have a number so you can parse it entry = parseInt(entry); // convert to number // now check for a valid number if (entry >= 0 && entry <= 100) { // number is in valid range check each grade if (entry < 50) { letterGrade = "F"; }else if (entry < 70) { letterGrade = "D"; }else if (entry < 80) { letterGrade = "C"; }else if (entry < 90) { letterGrade = "B"; }else if (entry <= 100) { letterGrade = "A"; } total += entry; // add the total entryCount += 1; // add ont to count // display entry and grade alert("Number grade = " + entry + "\nLetter grade = " + letterGrade); }else // if not in valid range check if its 999 if (entry != 999) { alert(errorString); // no show error } } }while (entry != 999); // do this till 999 is entered </script> </body> </html>

El código en sí está haciendo lo que se supone que debe hacer, pero de acuerdo con la asignación, tengo que llamar a una función como

 function myFunction(){}

No estoy loco por presionar 999 para romper el aviso, si alguien tiene alguna sugerencia al respecto, también sería muy apreciada.

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

0

Aquí estoy llamando a la función normalmente. Puede agregar esta función para que se llame en un determinado evento, como la carga de la página o el clic del botón.

 <!DOCTYPE html> <html> <body> <script> function myFunction() { var entry; var letterGrade; var total = 0; // you forgot to set the start value for total var entryCount = 0; // also for entry count you must initialize first. // add the error string here so we dont have to duplicate it in the code. var errorString = "Entry must by a valid number from 0 through 100\nOr enter 999 to end entries"; do { // get something from user entry = prompt("Enter number grade from 0 through 100\nOr enter 999 to end entries", 999); // the user may also enter something that is not a number if (isNaN(entry)) { // check if it is not a number alert(errorString); //show error if not a number } else { // you now know you have a number so you can parse it entry = parseInt(entry); // convert to number // now check for a valid number if (entry >= 0 && entry <= 100) { // number is in valid range check each grade if (entry < 50) { letterGrade = "F"; } else if (entry < 70) { letterGrade = "D"; } else if (entry < 80) { letterGrade = "C"; } else if (entry < 90) { letterGrade = "B"; } else if (entry <= 100) { letterGrade = "A"; } total += entry; // add the total entryCount += 1; // add ont to count // display entry and grade alert("Number grade = " + entry + "\nLetter grade = " + letterGrade); } else // if not in valid range check if its 999 if (entry != 999) { alert(errorString); // no show error } } } while (entry != 999); // do this till 999 is entered } myFunction(); </script> </body> </html>

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!