Print different data from an array according to the element value

Asked

Viewed 799 times

0

We have a list of all the family members forgot to put their last name!.

Your challenge will be to print name along with the surname "Macedo" to each member of the family. But we have an exception, we have one member with a different surname, if you have any "Pedro" "Sousa".

Tip: Use for and continue to create the solution. Add a space before the last name, for example: "Sousa".

Code that I made:

var familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"] 
var familia2 = " " 
for (i = 0; i < familia.length; i++){ 
    if( familia[i] == 'Pedro' ){ 
        console.log(familia[i] + ' Sousa') 
        continue 
    } 
    else if(familia[i] == 'Pedro'){ 
        console.log(familia[i] + ' Macedo')  
    }  
 }

  • var familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"] var familia2 = " " for (i = 0; i < familia.length; i++){ if( familia[i] == 'Pedro' )ľ console.log(familia[i] + ' Sousa') continue } Else if(familia[i] == 'Pedro'){ }console.log(familia[i] + 'Macedo') } exempl of one of the codes. This n has a lot of logic! however I made some more correct, even so the n terminal accepted

  • I put the code you wrote in comment on the question, as you should always do. I take this opportunity to ask also if it was correct, IE if it turned out like you have in your editor. Another situation that is not very clear to me is what you mean by "terminal n accepted"?

3 answers

3

The problem is in your if:

if( familia[i] == 'Pedro' ){ 
    console.log(familia[i] + ' Sousa') 
    continue 
}  else if(familia[i] == 'Pedro'){ 
    console.log(familia[i] + ' Macedo')  
}

First you test whether the name is "Peter". Then else you test if you are "Peter" again. It makes no sense to test the same thing twice.

In addition, the continue within the if is unnecessary. Using only if and else you already ensure that only one of the paths is executed.

According to the rule of the exercise, what you should do is: if the name is "Pedro", surname is "Sousa". Otherwise (for any name other than "Peter") the surname is "Macedo". So it would look like this:

var familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"];
for (var i = 0; i < familia.length; i++) {
    if (familia[i] == 'Pedro') { // se o nome é Pedro, o sobrenome é Souza
        console.log(familia[i] + ' Sousa');
    } else { // senão (se o nome não é Pedro), o sobrenome é Macedo
        console.log(familia[i] + ' Macedo');
    }
}

I also removed the variable familia2 that was not being used for anything. And I put a semicolon at the end of the lines - I know it’s optional (Javascript doesn’t complain if it doesn’t have one) and it might seem "fresh," but it avoids some bizarre situations that can occur if you don’t use them, like that one and that one (see more about this here).


Another way to do it is to use one for...of to traverse the array, and strings template to print:

var familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"];
for (var nome of familia) {
    if (nome == 'Pedro') {
        console.log(`${nome} Sousa`);
    } else {
        console.log(`${nome} Macedo`);
    }
}

Or else the forEach, as suggested in another answer.


About the use of continue

Exercise gives the "hint" to use continue, but I find it completely unnecessary. The idea of continue is to go to the next iteration of for, ignoring anything that comes after.

Then it would look like this:

var familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"];
for (var i = 0; i < familia.length; i++) {
    if (familia[i] == 'Pedro') { // se o nome é Pedro, o sobrenome é Souza
        console.log(familia[i] + ' Sousa');
        continue;
    }

    console.log(familia[i] + ' Macedo');
}

That is, if you enter the if, he prints "Pedro Souza" and continue makes it go to the next iteration of for (in this case, it will go to the name "Bruno"). If the name is not "Pedro", it does not enter the if and prints the surname "Macedo".

But I find it an unnecessary complication, just to force the use of continue. I still find the solution with if/else simpler and clearer.

1

Try it here buddy

const familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"]

familia.forEach(nome => {
  console.log(`${nome} ${nome === 'Pedro' ? 'Sousa' : 'Macedo'}`)
})

0

As in the exercise asked the use of continue, did so

var familia = ["Joana", "Felipe", "Gabriela", "Carlos", "Pedro", "Bruno"]

for (var i = 0; i < familia.length; i++){
    if(familia [i] == "Pedro") {
        console.log(familia[i] + " Sousa")
        continue
    }
    console.log( familia[i] + " Macedo")
}

Browser other questions tagged

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