How to make Javascript return a given result using the for.. of loop and the Join method?

Asked

Viewed 255 times

-2

I have a certain Javascript exercise that I can’t solve.

Given the following array object:

var usuarios = [
  {
    nome: "Diego",
    habilidades: ["Javascript", "ReactJS", "Redux"]
  },
  {
    nome: "Gabriel",
    habilidades: ["VueJS", "Ruby on Rails", "Elixir"]
  }
];

Write a function that produces the following result:

Diego has the skills: Javascript, Reactjs, Redux .
Gabriel has the skills: Vuejs, Ruby on Rails, Elixir.

I mean, I need you to console.log shows what was shown above, but it is necessary that to go through the array using the loop of repetition for..of and unite values using the Array.prototype.join.

  • 2

    What is the difficulty? You yourself have described what needs to be done to solve the problem: create a loop for and use the method join in the array.

2 answers

1

Note: Here at Sopt are not welcome questions of the style "do this homework for me", but questions of who wants to learn are very welcome.


Schematically what you need to solve this problem is, by steps:

  • iterate the user array

You can use the for ... of to iterate the array and use usuario as the name of the variable that each iteration receives

  • concatenate a string where you use the "abilities" and "name" properties of each object within the array

declares a variable where the value assigned is the join/concatenation. You can use strings template or concatenate the old-fashioned way: 'String' + variável + 'String'.

  • make a console.log of that string

console.log(minhaString);


3 steps.

-1

var usuarios = [
 {
 nome: "Diego",
 habilidades: ["Javascript", "ReactJS", "Redux"]
 },
 {
 nome: "Gabriel",
 habilidades: ["VueJS", "Ruby on Rails", "Elixir"]
 }
];


for( i of usuarios ) console.log( `O ${ Object.values( i )[0] } possui as habilidades: ${ Object.values( i )[1].join(', ') }.` )

/*
O Diego possui as habilidades: Javascript, ReactJS, Redux.
O Gabriel possui as habilidades: VueJS, Ruby on Rails, Elixir.
*/

Browser other questions tagged

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