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

147
Visualizações
trying to turn my working code into a function

I am trying to turn this working code I have into a function for a course I am taking.

<!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>

The code itself is doing what it is supposed to do but according to the assignment I have to call a function like

function myFunction(){}

I am not crazy about hitting 999 to break the prompt either if anyone has any suggestions bout that as well it would be greatly appreciated

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here I am calling function normally. You can add this function to be called on a certain event like page load or button click.

<!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 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