Help in JS arrays

Asked

Viewed 201 times

0

People I’m doing a course and I can’t get past this exercise.

Write the function "medalText(number)", which takes a number as a parameter. Using only the maximum of an "if" only. Maybe arrays can be useful here.

You have to return the medal that corresponds to the first positions of a competition:

medalleDecordoComplesto (1) "gold" medalleDecordoComplesto (2) "silver" medalleDecordoComsto (3) "bronze" medalleDecordoComsto (4) "nothing" medalleDecordoComsto (5) "nothing"

function medalhaDeAcordoComPosto (numero){
    var medalhas = ["ouro","prata", "bronze"];
    numero= numero -1;
    if (medalhas[numero] = "undefined"){
        return "nada"
    }else{
        return medalhas[numero];
    }

}

I’m using this code, but I get an error: The medal functionDecordoComPost(1) must return "gold" The medal functionDecordoComPost(2) must return "silver" The medal functionDecordoComPost(3) must return "bronze"

As far as I’m concerned, could someone help me understand what’s wrong with this code? Thank you very much.

  • You are assigning a value on if when using =, use == or === for comparisons

  • Even, have a question about this exercise if you have other questions, take a look there: Exercise taking the position of the array

  • So Raphael, I put the second=. 
function medalhaDeAcordoComPosto (numero){
 var medalhas = ["ouro","prata", "bronze"];
 numero= numero -1;
 if (medalhas[numero] == "undefined"){
 return "nada"
 }else{
 return medalhas[numero];
 }

}
 But now other errors appear: The medallion functionDeAgreementPost(4) should return "nothing" The medallion functionDecordoComplePost(45) should return "nothing" I saw your code and understood it, but I wanted to understand why mine is wrong yet, you could help me?

1 answer

1

The mistake is in:

medalhas[numero] = "undefined"

When using only one = instead of == you are assigning the value to variable and not comparing.

I also suggest checking the function index, which may be useful in solving this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.