Exercise taking the position of the array

Asked

Viewed 3,758 times

5

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

Write the function "medalText(number)", which takes a number as a parameter, using only an "if".

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

  • medalleDecordoComsto (1) - "gold"
  • medalleDecordoComsto (2) - "silver"
  • medalleDecordoComsto (3) - "bronze"
  • medalleDecordoComPost (4) - "nothing"
  • medalleDecordoComPost (15) - "nothing"
  • medalleDecordoComsto (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 higher than 3.

  • 1

    Actually, Undefined returns yes.

  • 2

    I created a snippet with the code you posted and when you run it you will see the return undefined, contradicting the text of your question. This will require you to edit the question and elaborate a [mcve] that demonstrates the behavior you are describing.

8 answers

7


Hello @Raphael, it seems to me that your exercise aims to make you understand two things.

  1. That the indexes have their beginning in 0.
  2. Identify/Work with the size of a Array.
    Array.length

I see the first you ever killed,
as it is placing as the first element of its Array the string "nada"

So for you to finish your exercise just you do the checking (if) using the array size (posicoes.length).

Important detail, posicoes.length is the amount of elements in your Array posicoes, is not the index of the last element.
Knowing this just add to 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 if a check for negative numbers, leaving +/- like this: if( numero >= posicoes.length || numero < 0)


Now let’s use the index 0

In this other example we will create an array only with prizes, so we will have:

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

In your job we’ll do the following:

  • Diminish 1 in the reported value.
    That way if the person calls the function by passing the 1 he will treat as 0.
  • Make a if to verify:

    • if the value is less than 0, that is, negative index and invalidity.
    • Or if the value is greater than to amount 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));

  • 2

    Thank you very much for your help and excellent explanation, Icaro. I haven’t reached the 15 points of reputation yet, but as soon as I can I’ll give +1 for the answer

5

Icaro Martins' answer is correct and perfectly answers the question. However, for those interested, I just wanted to add a minimalist solution using `||` and a Arrow Function. Check out:

const medalhaDeAcordoComPosto = numero => posicoes[numero] || "nada";
  • Thank you also, v. Santos. It is a very lean solution, I just need to study more about Arrow Function. If you have any material available, I would be grateful again :)

  • @Raphaelbarreto, don’t worry if you don’t understand the syntax. Keep studying the basics and when you feel safe with the basics it will be easier to understand Arrow functions. I indicate the site of the mozila: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions. Ah! Icaro’s answer is very good, I think you should accept it.

3

Follow my solution to the problem:

var posicoes = ['nada', 'ouro', 'prata', 'bronze'];

function medalhaDeAcordoComPosto(numero){
   if( numero >= posicoes.length ){
     return ("nada");
   } else{
     return posicoes[numero];    
   } 
  
}


console.log(medalhaDeAcordoComPosto(100));

2

Bro, I picked up your know why I just didn’t know how to make the start and worked with if and Else to treat numbers <0 and >3 binds:

var medal = ["nada", "ouro", "prata", "bronze"];

function medalhaDeAcordoComPosto(num){
    if (num >3 || num <0){
    return medal[0];
  }
  else{
    return medal[num];
  }
}

2

Hello, Raphael!

Then... The function you created returns an element of the array. Therefore, the (number) of the function must be a value related to the position of an array.

For example, the value "gold" is at position 1 of the array. So, if you want to print the "gold" element on the screen, you have to put the code as follows:

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

function medalhaDeAcordoComPosto(numero){
  return posicoes[numero];
}

console.log(medalhaDeAcordoComPosto(1));
  • Thanks for the help, Daniel. And already asking again, how would I do if I typed an element out of the array? For example, if I run console.log(medalDeAgreementPost(4)) will appear as Undefined. But what if I want to return the "nothing) option"?

  • Then you would have to put the number of the position corresponding to the element "nothing" which in this case would be the position 0. Then the console.log would look like this: console.log(medalDeAgreementPost(0); "gold" and put the 0 that corresponds to the position of the element "nothing". If you have any questions, you can send another question without any problems.

1

Follows a solution successfully executed:

function medalhaDeAcordoComPosto(numero){

  var posto=["ouro","prata","bronze","nada"];

  var posicao=numero-1;

  if (numero ==0 || numero >= posto.length){
     posicao=3;
    return posto[posicao];
  } else{

  return posto[posicao];
  }
}

1

function medalhaDeAcordoComPosto(numero) {
  var medalhas = ['nada', 'ouro', 'prata', 'bronze', 'nada', 'nada']

  if(medalhas.indexOf(medalhas[numero]) == numero) {
    return medalhas[numero]
  } else {
    return medalhas[3, 4]
  }
}

I do not know if you have already solved, but follows resolution... (I hope I helped) Hug!

0

I would do so...

function medalhaDeAcordoComPosto(posto){
var medalhas = ["nada","ouro","prata","bronze"];
if((posto > 0) && (posto < 4)){
    return console.log(medalhas[posto]);
}else{
    return console.log(medalhas[0]);
}

}

Browser other questions tagged

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