Javascript beginner in programming

Asked

Viewed 159 times

0

Help me execute this code correctly please!

Now that you know enough Javascript, the jury of a programming asked us to develop the medallion function which returns the medal corresponding to the positions, according to the following logic:

first place: corresponds to "gold"second place: corresponds to "silver" third place: corresponds to "bronze" other places: corresponds "Continue participating"

If we execute the function with the following positions, the result would be thus:

medallion GunPost(1) "gold" medallion Gunpowder(2) "silver" medalleSecondOPost(3) "bronze" medalleSecondOPost(5) "Continue participating"

Set the medallion functionSecond and returns a text according to the parameter. Tip: in this function you can use several if.

I wrote the following code:

var posicoes = ["continue participando", "ouro", "prata", "bronze"];

function medalhaSegundoOPosto(numero) {
    if (numero >= posicoes.length) {
        return "continue participando";
    } {
        return posicoes[numero];
    }
}
console.log(medalhaSegundoOPosto(1));
console.log(medalhaSegundoOPosto(2));
console.log(medalhaSegundoOPosto(3));
console.log(medalhaSegundoOPosto(4));
console.log(medalhaSegundoOPosto(9));

When run appears the following errors:

Execution of the medallion functionSecondOPost(4) must return 'Continue participating'

Execution of the medallion functionSecondOPost(9) must return 'Continue participating'

  • Continue participando is different from continue participando

  • I copied and pasted the code you wrote. I executed it. It worked perfectly. Using console.log(medalNumberOPost()) with parameters from 0 to 9, the output was exactly as expected: { 0 : 'continue participating', 1 : 'gold', 2 : 'silver', ... } and anyone from the third returns 'continue participating''.

1 answer

0

Let’s start with an explanation about arrays. An array is a variable that can receive several values that are separated by indices. An array will always start at 0

We can make your code from the following situation:

var posicoes = ['Continue Participando', 'Ouro', 'Prata', 'Bronze'];
  • At 0 we have 'Continue Participating'
  • In 1 we have 'Gold'
  • In 2 we have 'Silver'
  • In 3 we have 'Bronze

So in your function we can use the switch, which is similar to IF but you use when you already know which values will be declared.

function medalhaSegundoOPosto(numero) {
    switch(numero) {
        case 0:
        case 1:
        case 2:
            return posicoes[numero];
        break;
        default:
            return posicoes[0];
        break;
    }
}

Running the function, finally:

console.log(medalhaSegundoOPosto(1));
//Ouro
console.log(medalhaSegundoOPosto(2));
//Prata
console.log(medalhaSegundoOPosto(3));
//Bronze
console.log(medalhaSegundoOPosto(4));
//Continue Participando
console.log(medalhaSegundoOPosto(0));
//Continue Participando
console.log(medalhaSegundoOPosto(10));
//Continue Participando

Explaining the switch:

Switch checks the condition and returns a value according to what has been set. In your case we made a number switch and added the 'cases':

  • If the value is 1 (case 1), or 2 (case 2) or 3 (case 3), will return on the console the medal according to the number sent (positions[number]).
  • If any other number (0 or 4 up) will call the joker default, which is when no switch condition was satisfied. In this case it will display index 0 of its variable.
  • Every case needs to end with a break, otherwise it will move on to the next instruction

And at the end an example where will display on the screen its function:

var posicoes = ["continue participando", "ouro", "prata", "bronze"];

function medalhaSegundoOPosto(numero) {
switch(numero) {
case 0:
case 1:
case 2:
return posicoes[numero];
break;
default:
return posicoes[0];
break;
}
}

document.querySelector("#medalha").innerHTML =  medalhaSegundoOPosto(1);
<div id="medalha">


</div>

Browser other questions tagged

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