javascript – Exercise getting the position of the array

Question:

I'm doing an exercise but I can't go through all the necessary validations.

Write the function “MedalhaDeAcordoComPosto(number)”, which receives a number as a parameter, using only an “if”.

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

• MedalDeagredoComPosto (1) – "gold"
• MedalDeagredoComPosto (2) – "silver"
• MedalDeagredoComPosto (3) – "bronze"
• MedAgreementWithPost (4) – "nothing"
• MedAgreementWithPost (15) – "nothing"
• MedAgreementWithPost(0) – "nothing"
` `var posicoes = ["nada", "ouro", "prata", "bronze"]; function medalhaDeAcordoComPosto(numero){ return posicoes[numero]; } console.log(medalhaDeAcordoComPosto(15));``

My biggest question is how to return "nothing" when I pass parameter 0 or greater than 3.

Hi @Raphael, it seems to me that your exercise is intended to make you understand two things.

1. That the indices start at `0` .
2. Identify/Work with the size of an `Array` .
`Array.length`

I see that the first you already killed ,
because it is placing as the first element of its Array the string `"nada"`

So for you to finish your exercise you just have to check ( `if` ) using the size of the array ( `posicoes.length` ).

Important detail, `posicoes.length` is the number of elements in your Array `posicoes` , not the index of the last element .
Knowing this, just add the check `if( numero >= posicoes.length ) return "nada";`

` `var posicoes = ["nada", "ouro", "prata", "bronze"]; function medalhaDeAcordoComPosto(numero){ if( numero >= posicoes.length ) return "nada"; return posicoes[numero]; } console.log(0,medalhaDeAcordoComPosto(0)); console.log(1,medalhaDeAcordoComPosto(1)); console.log(2,medalhaDeAcordoComPosto(2)); console.log(3,medalhaDeAcordoComPosto(3)); console.log(4,medalhaDeAcordoComPosto(4)); console.log(5,medalhaDeAcordoComPosto(5)); console.log(15,medalhaDeAcordoComPosto(15));``

Note: You may need to add a check for negative numbers to the `if` , leaving +/- like this: `if( numero >= posicoes.length || numero < 0)`

Now let's use index `0`

In this other example we are going to create an array with just the prizes, so we will have:

``````var posicoes = ["ouro", "prata", "bronze"];
``````

In your function we will do the following:

• Decrease by `1` the value entered.
That way if the person calls the function passing the `1` it will treat it as `0` .
• Make an `if` to check:

• if the value is less than `0` , that is, negative and invalid index.
• OR if the value is greater than the number of elements in the array `- 1` , that is, the last index of the array. `(posicoes.length-1)`
` `var posicoes = ["ouro", "prata", "bronze"]; function medalhaDeAcordoComPosto(numero){ numero -= 1; /// ; isso é a msm coisa que `numero = numero - 1;` if( numero < 0 || numero > posicoes.length-1 ) return "nada"; return posicoes[numero]; } console.log(0,medalhaDeAcordoComPosto(0)); console.log(1,medalhaDeAcordoComPosto(1)); console.log(2,medalhaDeAcordoComPosto(2)); console.log(3,medalhaDeAcordoComPosto(3)); console.log(4,medalhaDeAcordoComPosto(4)); console.log(5,medalhaDeAcordoComPosto(5)); console.log(15,medalhaDeAcordoComPosto(15));``
Scroll to Top