-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from LarissaCns/exercicios-8.6
Praticas dia 6
- Loading branch information
Showing
16 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// 1 - Dado o código abaixo, complete-o de forma que seja impressa a área dos 3 retângulos. O código deve retornar em sequência 2 , 15 e 54 . | ||
|
||
const rectangleArea = (width, height) => width * height; | ||
|
||
const rectangle1 = [1, 2]; | ||
const rectangle2 = [3, 5]; | ||
const rectangle3 = [6, 9]; | ||
const rectangles = [rectangle1, rectangle2, rectangle3]; | ||
|
||
rectangles.forEach((rectangle) => { | ||
rectangleArea(...rectangle) | ||
console.log(rectangle[0] * rectangle[1]); | ||
}); |
3 changes: 3 additions & 0 deletions
3
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const soma = (...numeros) => numeros.reduce(((acc,val) => acc + val),0); | ||
|
||
console.log(soma(1,2,3,4,5)) |
21 changes: 21 additions & 0 deletions
21
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 3 - Escreva a função personLikes , que dado um objeto de parâmetro que representa uma pessoa, retorna todos os gostos daquela pessoa, conforme mostrado abaixo: | ||
const alex = { | ||
name: 'Alex', | ||
age: 26, | ||
likes: ['fly fishing'], | ||
nationality: 'Australian', | ||
}; | ||
|
||
const gunnar = { | ||
name: 'Gunnar', | ||
age: 30, | ||
likes: ['hiking', 'scuba diving', 'taking pictures'], | ||
nationality: 'Icelandic', | ||
}; | ||
|
||
// complete a assinatura da função abaixo | ||
const personLikes = ({name,age,likes}) => `${name} is ${age} years old and likes ${likes.join(', ')}.`; | ||
|
||
console.log(personLikes(alex)) // 'Alex is 26 years old and likes fly fishing.' | ||
console.log(personLikes(gunnar)) // 'Gunnar is 30 years old and likes hiking, scuba diving, taking pictures.' | ||
|
36 changes: 36 additions & 0 deletions
36
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio4.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 4 - Escreva uma função filterPeople que, dada uma lista de pessoas, retorna todas as pessoas australianas que nasceram no século 20: | ||
|
||
const people = [ | ||
{ | ||
name: 'Nicole', | ||
bornIn: 1992, | ||
nationality: 'Australian', | ||
}, | ||
{ | ||
name: 'Harry', | ||
bornIn: 2008, | ||
nationality: 'Australian', | ||
}, | ||
{ | ||
name: 'Toby', | ||
bornIn: 1901, | ||
nationality: 'Australian', | ||
}, | ||
{ | ||
name: 'Frida', | ||
bornIn: 1960, | ||
nationality: 'Dannish', | ||
}, | ||
{ | ||
name: 'Fernando', | ||
bornIn: 2001, | ||
nationality: 'Brazilian', | ||
}, | ||
]; | ||
|
||
// escreva filterPeople abaixo | ||
|
||
const filterPeople = (array) => array.filter(({nationality, bornIn}) => nationality === 'Australian' && bornIn > 1900 && bornIn <= 2000, | ||
); | ||
|
||
console.log(filterPeople(people)); |
6 changes: 6 additions & 0 deletions
6
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio5.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// 5 - Escreva a função swap , que dado um array de 3 elementos, retorna um novo array com o primeiro e terceiro elementos trocados. Detalhe: você precisa fazer essa função gastando 1 linha só: | ||
|
||
const myList = [1, 2, 3]; | ||
|
||
// escreva swap abaixo | ||
const swap = ([a,b,c]) => [c,b,a]; |
10 changes: 10 additions & 0 deletions
10
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio6.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Suponha que você esteja lidando com carros e, da forma como o problema lhe foi entregue, cada carro é modelado como um array. Porém, essa modelagem está baixo nível. Cria uma função toObject que, dada uma lista, retorna um objeto representando o carro: | ||
|
||
const palio = ['Palio', 'Fiat', 2019]; | ||
const shelbyCobra = ['Shelby Cobra', 'Ford', 1963]; | ||
const chiron = ['Chiron', 'Bugatti', 2016]; | ||
|
||
// escreva toObject abaixo | ||
const toObject = ([name,brand,year]) => ({name,brand,year}); | ||
|
||
console.log(toObject(palio)); |
27 changes: 27 additions & 0 deletions
27
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio7.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Escreva uma função shipLength que, dado um objeto representando um navio, retorna o comprimento dele, mostrando também a devida unidade de comprimento: | ||
const ships = [ | ||
{ | ||
name: 'Titanic', | ||
length: 269.1, | ||
measurementUnit: 'meters', | ||
}, | ||
{ | ||
name: 'Queen Mary 2', | ||
length: 1132, | ||
measurementUnit: 'feet', | ||
}, | ||
{ | ||
name: 'Yamato', | ||
length: 256, | ||
measurementUnit: 'meters', | ||
}, | ||
]; | ||
|
||
// escreva shipLength abaixo | ||
const shipLength = ({name,length,measurementUnit}) => { | ||
return `${name} is ${length} ${measurementUnit} long`; | ||
}; | ||
|
||
console.log(shipLength(ships[0])) // 'Titanic is 269.1 meters long' | ||
console.log(shipLength(ships[1])) // 'Queen Mary 2 is 1132 feet long' | ||
console.log(shipLength(ships[2])) // 'Yamato is 256 meters long' |
8 changes: 8 additions & 0 deletions
8
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio8.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//8 - Escreva uma função greet que, dado o nome de uma pessoa, retorna uma mensagem de cumprimento: | ||
|
||
// escreva greet abaixo | ||
const greet = (name,greeting = 'Hi') => `${greeting} ${name}`; | ||
|
||
console.log(greet('John')) // 'Hi John' | ||
console.log(greet('John', 'Good morning')) // 'Good morning John' | ||
console.log(greet('Isabela', 'Oi')) // 'Oi Isabela' |
13 changes: 13 additions & 0 deletions
13
...order-functions/dia-6-spreadOperator-parametroRest-destructuring/exercicios/exercicio9.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// 9 - Existe um objeto yearSeasons que representam as estações do ano. Cada estação é uma chave deste objeto e o valor de cada chave é um array de strings, representando os meses daquela estação. A partir deste objeto, desestruture as estações do ano e espalhe-as em um array de meses do ano. | ||
|
||
const yearSeasons = { | ||
spring: ['March', 'April', 'May'], | ||
summer: ['June', 'July', 'August'], | ||
autumn: ['September', 'October', 'November'], | ||
winter: ['December', 'January', 'February'], | ||
}; | ||
|
||
const {spring,summer,autumn,winter} = yearSeasons; | ||
const months = [...spring,...summer,...autumn,...winter]; | ||
|
||
console.log(spring); |
42 changes: 42 additions & 0 deletions
42
...functions/dia-6-spreadOperator-parametroRest-destructuring/praticas/ArrayDestructuring.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const arrayCountries = ['Brazil', 'Japan', 'China', 'Canada']; | ||
const [firstCountry, secondCountry, thirdCountry, fourthCountry] = arrayCountries; | ||
|
||
console.log(firstCountry); // Brazil | ||
console.log(secondCountry); // Japan | ||
console.log(thirdCountry); // China | ||
console.log(fourthCountry); // Canada | ||
|
||
//---------------------- | ||
|
||
// Para fixar | ||
// 1 - Produza o mesmo resultado do código, porém utilizando o array destructuring para recolher a função e a saudação. | ||
|
||
const saudacoes = ['Olá', (saudacao) => console.log(saudacao)]; | ||
|
||
saudacoes[1](saudacoes[0]); // Olá | ||
|
||
// Produza o mesmo resultado acima, porém utilizando array destructuring | ||
const [saudacao, imprimiSaudacao] = saudacoes; | ||
console.log(imprimiSaudacao(saudacao)); | ||
|
||
// ------------- | ||
// 2 - A seguir, temos alguns valores que estão descritos em variáveis incoerentes. Através da desestruturação de arrays, corrija os valores das variáveis. | ||
let comida = 'gato'; | ||
let animal = 'água'; | ||
let bebida = 'arroz'; | ||
|
||
//console.log(comida, animal, bebida); // arroz gato água | ||
|
||
// Utilizando array destructuring, faça com que os valores apareçam nas variáveis correspondentes ao seu verdadeiro tipo | ||
|
||
[comida, animal, bebida] = [bebida,comida,animal]; | ||
console.log(comida,animal,bebida); | ||
|
||
// 3 - array abaixo possui alguns números que não condizem com o conteúdo que ele deveria possuir. Através de array destructuring, faça com que existam apenas números pares na variável numerosPares . | ||
|
||
let numerosPares = [1, 3, 5, 6, 8, 10, 12]; | ||
|
||
// Utilize array destructuring para produzir o resultado esperado pelo console.log abaixo | ||
[,,, ...numerosPares] = numerosPares; | ||
console.log(numerosPares); // [6, 8, 10, 12]; | ||
|
30 changes: 30 additions & 0 deletions
30
...nctions/dia-6-spreadOperator-parametroRest-destructuring/praticas/DefaultDestructuring.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const person = { | ||
name: 'João', | ||
lastName: 'Jr', | ||
age: 34, | ||
}; | ||
|
||
const { nationality } = person; | ||
|
||
console.log(nationality); | ||
|
||
// ------------------- | ||
// Para Fixar | ||
|
||
//Do jeito que está, quando passamos person para a função GetNationality o retorno é João is undefined . Ajuste a função GetNationality para que a chamada GetNationality(person) retorne João is Brazilian . | ||
|
||
const getNationality = ({ firstName, nationality = 'Brazilian' }) => `${firstName} is ${nationality}`; | ||
|
||
const person = { | ||
firstName: 'João', | ||
lastName: 'Jr II', | ||
}; | ||
|
||
const otherPerson = { | ||
firstName: 'Ivan', | ||
lastName: 'Ivanovich', | ||
nationality: 'Russian', | ||
}; | ||
|
||
console.log(getNationality(otherPerson)); // Ivan is Russian | ||
console.log(getNationality(person)); |
12 changes: 12 additions & 0 deletions
12
...-functions/dia-6-spreadOperator-parametroRest-destructuring/praticas/DefaultParameters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const greeting = (user = 'usuário') => console.log(`Welcome ${user}!`); | ||
|
||
greeting(); // // Welcome usuário! | ||
|
||
// Para Fixar | ||
// Para praticar, escreva uma função multiply que multiplique dois números passados como argumentos. Atribua como default o valor 1 caso não seja passado nenhum valor como segundo parâmetro. | ||
|
||
const multiply = (number = 1, value = 1) => { | ||
return number * value; | ||
}; | ||
|
||
console.log(multiply(8)); |
54 changes: 54 additions & 0 deletions
54
...unctions/dia-6-spreadOperator-parametroRest-destructuring/praticas/ObjectDestructuring.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const daysOfWeek = { | ||
workDays: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], | ||
weekend: ['Saturday', 'Sunday'], | ||
}; | ||
|
||
const { workDays, weekend } = daysOfWeek; //desestruturação | ||
console.log(workDays); | ||
// ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] | ||
console.log(weekend); | ||
// ['Saturday', 'Sunday'] | ||
|
||
const weekdays = [...workDays, ...weekend]; //spread operator | ||
console.log(weekdays); | ||
// ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | ||
|
||
// --------------------- | ||
const product = { | ||
name: 'Smart TV Crystal UHD', | ||
price: '1899.05', | ||
seller: 'Casas de Minas', | ||
}; | ||
|
||
const printProductDetails = ({ name, price, seller }) => { | ||
console.log(`Promoção! ${name} por apenas ${price} é só aqui: ${seller}`); | ||
}; | ||
|
||
printProductDetails(product); // Promoção! Smart TV Crystal UHD por apenas 1899.05 é só aqui: Casas de Minas | ||
|
||
// -------------------------- | ||
// Para Fixar - | ||
// 1 - Temos dois objetos, um com informações pessoais de uma pessoa usuária e outro com informações referentes ao cargo desta pessoa usuária na empresa trappistEnterprise . Precisamos criar um terceiro objeto, que terá os dados pessoais e os dados de cargo juntos. Para isso, utilize o spread operator . | ||
|
||
const user = { | ||
name: 'Maria', | ||
age: 21, | ||
nationality: 'Brazilian', | ||
}; | ||
|
||
const jobInfos = { | ||
profession: 'Software engineer', | ||
squad: 'Rocket Landing Logic', | ||
squadInitials: 'RLL', | ||
}; | ||
|
||
const userJob = {...user,...jobInfos}; | ||
|
||
// 2 - Com o objeto em mãos, imprima no console uma frase utilizando os dados do objeto criado anteriormente. Para isso, utilize a desestruturação de objetos em conjunto com template literals . | ||
|
||
const {name,age,nationality,profession,squad,squadInitials} = userJob; | ||
console.log(`Hi, my name is ${name}, I'm ${age} years old and I'm ${nationality}. I work as a ${profession} and my squad is ${squadInitials}-${squad}`); | ||
|
||
|
||
|
||
|
5 changes: 5 additions & 0 deletions
5
...ions/dia-6-spreadOperator-parametroRest-destructuring/praticas/ObjectPropertyShorthand.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getPosition = (latitude, longitude) => ({ | ||
latitude, | ||
longitude,}); | ||
|
||
console.log(getPosition(-19.8157, -43.9542)); |
14 changes: 14 additions & 0 deletions
14
...rder-functions/dia-6-spreadOperator-parametroRest-destructuring/praticas/ParametroRest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Utilizando o método length com parâmetro Rest | ||
|
||
function quantosParams(...args) { | ||
console.log('parâmetros:', args); | ||
return `Você passou ${args.length} parâmetros para a função.`; | ||
} | ||
|
||
console.log(quantosParams(0, 1, 2)); // Você passou 3 parâmetros para a função. | ||
console.log(quantosParams('string', null, [1, 2, 3], { })); // Você passou 4 parâmetros para a função. | ||
|
||
// --------------------------- | ||
const sum = (...args) => args.reduce((accumulator, current) => accumulator + current, 0); // soma todos os números do array | ||
console.log(sum(4, 7, 8, 9, 60)); // 88 | ||
|
50 changes: 50 additions & 0 deletions
50
...der-functions/dia-6-spreadOperator-parametroRest-destructuring/praticas/SpreadOperator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const imc = (peso, altura) => (peso / (altura * altura)).toFixed(2); | ||
const patientInfo = [60, 1.7]; | ||
|
||
console.log(imc(...patientInfo)); // 20.76 | ||
|
||
// ------------------------ | ||
|
||
const randomNumbers = [57, 8, 5, 800, 152, 74, 630, 98, 40]; | ||
|
||
console.log(Math.max(...randomNumbers)); // 800 | ||
console.log(Math.min(...randomNumbers)); // 5 | ||
|
||
// ------------------------- | ||
|
||
const people = { | ||
id: 101, | ||
name: 'Alysson', | ||
age: 25, | ||
}; | ||
|
||
const about = { | ||
address: 'Av. Getúlio Vargas, 1000', | ||
occupation: 'Developer', | ||
}; | ||
|
||
const customer = { ...people, ...about }; | ||
console.log(customer); /* { | ||
id: 101, | ||
name: 'Alysson', | ||
age: 25, | ||
address: 'Av. Getúlio Vargas, 1000', | ||
occupation: 'Developer' | ||
} */ | ||
|
||
// -------------------------- | ||
// Para fixar | ||
|
||
// Faça uma lista com as suas frutas favoritas | ||
const specialFruit = ['Morango', 'Uva', 'Maçã']; | ||
|
||
// Faça uma lista de complementos que você gostaria de adicionar | ||
const additionalItens = ['Melancia', 'Kiwi', 'Banana']; | ||
|
||
const fruitSalad = (fruit, additional) => { | ||
return [...fruit,...additional]; | ||
}; | ||
|
||
console.log(fruitSalad(specialFruit, additionalItens)); | ||
|
||
// -------------------------- |