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

174
Visualizações
I need to use Math.floor in a JavaScript program to round down the result of a grade calculation, but the examples I have seen or tried do not work

I know the syntax for Math.floor is a number so you can put a number inside of the statement as an argument or have a very equal a number, but in my case I have a grade variable being added to another variable called "total" and then the average of the grades entered equals the total divided by the amount of grades entered.

Here is my code.

var grade = 0;
var avg = 0;
var count = 0;
var total = 0;

do {
    grade = prompt('Enter grade(-1 to stop)');

       
    while (!grade || grade < -1 || grade > 100 || isNaN(grade)) {
      grade = prompt('Enter grade again (-1 to stop)');
    }

        
    if (grade == -1) {
      break
    }

    count++;

    document.write('<td>Grade ' + count + '</td>');
    document.write('<td width="50"> ' + grade + ' </td>');
    document.write('</tr>');

    total += Number(grade); 

  } while (grade != -1)

  document.write('</table>');

  if(count > 0) {
    avg = total / count;
    
  }
  //Math.floor would go here instead of document.write.
  document.write('Semester Average: ' + avg + '<br /><br />');

I know you can't use the average variable inside of Math.floor but there has to be another way to have the average rounded dwon if it isn't a whole number.

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

0

You can simply use Math.floor (or round) on the var before you write it.

const rounded = Math.round(avg)

about 4 years ago · Juan Pablo Isaza Relatório

0

Using document.write() is in most cases not a good choice since it causes the document to be left in an unfinished state. Here is my version of the script that will always give you a valid grade overview, even before you have finished collecting all the grades.

function tr(i, g) { // creates and returns a TR DOM element
  const el = document.createElement("tr");
  el.innerHTML = `<td>${i}.</td><td>${g}</td>`;
  return el;
}

const tb = document.querySelector("#mytbl tbody"),
  tf = document.querySelector("#mytbl tfoot th:nth-child(2)"),
  gr = document.querySelector("#grade");
gr.focus();                         // position cursor on input field

document.querySelector("form").addEventListener("submit", function(ev) {
  ev.preventDefault();
  const n = tb.children.length + 1; // calculate curent count
  tb.append(tr(n, gr.value));
  gr.value = "";                    // empty the input field again
  tf.textContent = ([...tb.querySelectorAll("td:nth-child(2)")]
    .reduce((a, c) => +c.textContent + a, 0) / n).toFixed(1) // calculate average and round to 1 digit
});
td, tfoot th:nth-child(2) {text-align:right}
<form><input type="text" id="grade" placeholder="Please enter a grade ..."></form>
<table id="mytbl">
  <thead>
    <tr>
      <th>no.</th>
      <th>grade</th>
    </tr>
  </thead>
  <tbody></tbody>
  <tfoot>
    <tr>
      <th>avg.:</th>
      <th></th>
    </tr>
  </tfoot>
</table>

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