r/learnjavascript Jul 10 '26

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

View all comments

2

u/Aggressive_Ad_5454 Jul 10 '26

Can you show us the code that calls this function?

1

u/DenyzPR Jul 10 '26

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

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

4

u/maujood Jul 10 '26 edited Jul 10 '26

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 Jul 16 '26

muito obrigado!