Loop Array - Fruit

Asked

Viewed 1,811 times

-1

I’m trying to do an exercise, but it wasn’t the way the platform wanted, I’ve already spent a few hours trying because I’m beginner and I have no ideas how to solve, could help me?

Enunciation:

A bag assembled a list of the fruits they sell, and according to the fruit the user seeks they want to inform if there is the fruit in the list or not!

var listaDeFrutas = [ "Uva", "Banana",  "Manga", "Cajá", "Pinha"]

You should create a loop that checks if the fruit contained in the search variable exists in the bag’s fruit list. If there is just display a message, "Yes, we have banana fruit available". Use the search variable to display the name of the fruit in this message dynamically.

What I wrote:

var listaDeFrutas = [ "Uva", "Banana",  "Manga", "Cajá", "Pinha"]
var busca = "Cajá"

//seu loop aqui

for (var i = 0; i < listaDeFrutas.length; i++ ){
    if (listaDeFrutas[i] = 3){
        
    }
}
    console.log("Sim, temos a fruta  disponível")

Error:

You must make a condition to verify that the value of the sought fruit is equal to the value being traversed from the array!

  • 2

    The Ars forgot to read "You must make a condional to check if the value of the search fruit is equal to the value being traversed from the array!"

6 answers

3

solved, rolled like this:

var listaDeFrutas = [ "Uva", "Banana",  "Manga", "Cajá", "Pinha"]
var busca = "Cajá"

//seu loop aqui

for (var i = 0; i < listaDeFrutas.length; i++){
    if(listaDeFrutas[i] == busca){
        console.log("Sim, temos a fruta Cajá disponível")
    }
}
  • But what if the fruit to be sought is not Cajá? It will show the same message?

  • Then I was looking for the cashew, as stated in the statement..

  • 1

    But if so busca were "Banana", for example, the message that would be displayed (being that way) would still be "Yes, we have the cashew fruit available".

  • So I’m taking the first classes, and I don’t know how to answer you. just doing the exercise as you requested

  • @Gustavosampaio, is a simple exercise that aims to compare a variable with the items of a vector, nothing more.

2

My code went like this:

var listaDeFrutas = ["Uva", "Banana",  "Manga", "Cajá", "Pinha"]
var busca = "Cajá"

for (i=0;i<listaDeFrutas.length;i++)
    if(listaDeFrutas[i]==busca){
        console.log("Sim, temos a fruta "+busca+"disponível")
    }

1

Your code has some problems considering the enunciation of the exercise:

  1. You are using an assignment operator (=) instead of comparison (== or ===) to check if the fruit is in the desired content;
  2. You’re checking the number 3 instead of checking the contents of your variable busca;

You can also create a boolean control variable with value false to indicate whether the value has been found or not, which will be used later to decide whether the message will be shown. With these changes we have the following code:

var listaDeFrutas = ["Uva", "Banana", "Manga", "Cajá", "Pinha"];
var busca = "Cajá";
var encontrado = false;

for (var i = 0; i < listaDeFrutas.length; i++) { // Você deverá criar um loop...
  if (listaDeFrutas[i] === busca) { // ...que verifique se a fruta contida na variável busca existe na lista de frutas do sacolão.
    encontrado = true;
  }
}

if (encontrado) { // Se existe...
  console.log(`Sim, temos a fruta ${busca} disponível`); // ...basta exibir uma mensagem. Use a variável busca para exibir o nome da fruta nessa mensagem de forma dinâmica.
}

1

Mine spun like this

for (var i = 0; i < listaDeFrutas.length; i++ ) {
  if (listaDeFrutas[i] == busca) {
    console.log("Sim, temos a fruta  disponível")
  }
}

1

I did it and it worked

var listaDeFrutas = [ "Uva", "Banana",  "Manga", "Cajá", "Pinha"]  
var busca = "Cajá"

for (var i = 0; i < listaDeFrutas.length; i++){  
    if(listaDeFrutas[i] == busca) {   
        console.log("Sim, temos a fruta " + " disponível");  
    }  
}

0

There are some functional operations in the array called filter and another called map, you could use it to get this result as follows:

let listaDeFrutas = [ "Uva", "Banana",  "Manga", "Cajá", "Pinha"];
let search = 'Cajá';

istaDeFrutas.filter(x => x === 'Cajá').map(x => `Sim, temos a fruta ${x} disponível`);

Just create a function with the filter code and use the return of this on your console.log()

I recommend this reading: https://imasters.com.br/desenvolvimento/principios-de-programacao-funcional-com-javascript

  • 1

    In fact your answer does not answer the question, because as the statement already says, "You must create a loop". If not to create one loop probably the best option would be to use a indexOf or includes.

Browser other questions tagged

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