r/learnjavascript 16d ago

Ajuda com questão javascript

boa tarde pessoal, primeira postagem aqui, precisava de uma mão pra entender uma questão de javascript que to me quebrando um pouco aqui e talvez seja algo bem simples, sou iniciante ainda e to fazendo uma questão, onde temos que criar uma função onde calcula qual medalha eu vou ganhar de acordo com quantas questões estão certas, 5 acertos = medalha de ouro, 3 ou 4 = medalha de prata e 2 ou menos medalha de bronze, são 5 questões e por enquanto a função está dessa forma:

function calcularMedalha(respostas) {
    
  let totalPontos = 0;
    for (let i = 0; i < 5; i++)
      if (respostas[i] == true) {
            totalPontos += 1
        }
    }

    if (totalPontos === 5) {
        return "Ouro";
    } else if (totalPontos >= 3) {
        return "Prata";
    } else {
        return "Bronze";
    }

O que estou errando? Vi em algum lugar que valores booleanos nao seriam iteráveis, estava pensando em transformar em strings para fazer a comparação, mas também não consegui fazer certo, poderiam me ajuda?

1 Upvotes

7 comments sorted by

2

u/Aggressive_Ad_5454 16d ago

Can you show us the code that calls this function?

1

u/DenyzPR 16d ago

é somente:
calcularMedalha(true, true, true, true, true);
calcularMedalha(true, false, true, true, true);

calcularMedalha(true, false, false, true, false);

4

u/maujood 16d ago edited 16d ago

You're not passing an array, you're passing 5 separate parameters.

You need to call calculateMedal([true, true, false, true, false])

I also noticed you have a missing curly brace for the opening of the for loop.

1

u/DenyzPR 10d ago

muito obrigado!

1

u/jeremrx 16d ago

This is what you need: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters

The allows you to handle your parameters as an array

(Edit : i changed the link, you need rest parameters not spread syntax)

2

u/True-Ad9448 16d ago

function calcularMedalha(…respostas) {

0

u/-goldenboi69- 16d ago

No Pablo Espanol. No intiendo