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

124
Visualizações
Calculating sum of fractions in a loop

Implement a function fractionSum(n) that calculates and returns the sum of the following sequence: 1/n + 1/(n−1) + 1/(n−2) + ⋅⋅⋅ + 1/2 + 1

For example, fractionSum(5) calculates the following sum: 1/5+1/4+1/3+1/2+1 And then returns 2.283333333333333

I haven't even started writing function just yet since I'm still stuck at trying to figure out the right loop expression. This is what I've done so far:

        var sum =0;
        var fraction = 1;
        
        var number = parseInt(prompt("Enter an integer: ", "5"));
       
        for ( var counter = 0; counter <= number; counter++) {
            fraction /= (number - counter); // do I need to declare parseFloat here for decimal# ?
            sum += fraction;
        }     
        document.write("The total value is " + sum);

The number doesn't match up at all from the example. I'm not sure what the problem is here.

I'm pretty confused right now. I know this is basic problem but I has tried multiple codes and it still didn't come out right.

Thank you so much

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

0

You're reusing the fraction from the previous iteration and dividing it by the next value. You need a new fraction instead:

fraction = 1 / (number - counter);

Also, you need the strict counter < number condition in the loop to avoid division by zero.

about 4 years ago · Juan Pablo Isaza Relatório

0

In the loop, counter must be less than number else at the end there will be divison by 0, the result will be infinity. Try this

let sum = 0;
let i = 0;
let num = parseInt(prompt("Enter an integer: ", "5"));
for(i; i < num; i++ ){
  frac = 1 / ( num - i);
  sum+= frac;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I figured it out. This is what I got:

<script>
    function fractionSum(number) {
        var sum =0;
        for (var counter= 0 ; counter < number; counter++) {   
            sum += 1/(number - counter);
        }  
        return sum;
    }

    var number = parseInt(prompt("Enter the value of n: ", "5"));
    sum = fractionSum(number);
    document.write("The fraction sum of order " + number + " is " + sum);
</script>

Thank you guys, it was very helpful

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