Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dia 8.4 #19

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bloco_8/dia_4/Exercicio_1/exercise1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

const assert = require('assert');

const arrays = [
['1', '2', '3'],
[true],
[4, 5, 6],
];

function flatten() {
// escreva seu código aqui
return arrays.reduce((arrayVazio, array) => arrayVazio.concat(array.filter((elemento) => elemento)), [])
}
assert.deepStrictEqual(flatten(), ['1', '2', '3', true, 4, 5, 6]);
82 changes: 82 additions & 0 deletions bloco_8/dia_4/Exercicio_1/exercise2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

const assert = require('assert');

const books = [
{
id: 1,
name: 'As Crônicas de Gelo e Fogo',
genre: 'Fantasia',
author: {
name: 'George R. R. Martin',
birthYear: 1948,
},
releaseYear: 1991,
},
{
id: 2,
name: 'O Senhor dos Anéis',
genre: 'Fantasia',
author: {
name: 'J. R. R. Tolkien',
birthYear: 1892,
},
releaseYear: 1954,
},
{
id: 3,
name: 'Fundação',
genre: 'Ficção Científica',
author: {
name: 'Isaac Asimov',
birthYear: 1920,
},
releaseYear: 1951,
},
{
id: 4,
name: 'Duna',
genre: 'Ficção Científica',
author: {
name: 'Frank Herbert',
birthYear: 1920,
},
releaseYear: 1965,
},
{
id: 5,
name: 'A Coisa',
genre: 'Terror',
author: {
name: 'Stephen King',
birthYear: 1947,
},
releaseYear: 1986,
},
{
id: 6,
name: 'O Chamado de Cthulhu',
genre: 'Terror',
author: {
name: 'H. P. Lovecraft',
birthYear: 1890,
},
releaseYear: 1928,
},
];

const comparison = (stringVazia, book, index) =>{
if (index == books.length - 1) {
stringVazia += ` ${book.author.name}.`
} else {
stringVazia += ` ${book.author.name},`
}
return stringVazia
}
const constructString = (stringVazia, book, index) => comparison(stringVazia, book, index);

function allNames() {
// escreva seu código aqui
return books.reduce(constructString, 'Nomes:')
}

assert.deepStrictEqual(allNames(), 'Nomes: George R. R. Martin, J. R. R. Tolkien, Isaac Asimov, Frank Herbert, Stephen King, H. P. Lovecraft.');
78 changes: 78 additions & 0 deletions bloco_8/dia_4/Exercicio_1/exercise3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const assert = require('assert');

const books = [
{
id: 1,
name: 'As Crônicas de Gelo e Fogo',
genre: 'Fantasia',
author: {
name: 'George R. R. Martin',
birthYear: 1948,
},
releaseYear: 1991,
},
{
id: 2,
name: 'O Senhor dos Anéis',
genre: 'Fantasia',
author: {
name: 'J. R. R. Tolkien',
birthYear: 1892,
},
releaseYear: 1954,
},
{
id: 3,
name: 'Fundação',
genre: 'Ficção Científica',
author: {
name: 'Isaac Asimov',
birthYear: 1920,
},
releaseYear: 1951,
},
{
id: 4,
name: 'Duna',
genre: 'Ficção Científica',
author: {
name: 'Frank Herbert',
birthYear: 1920,
},
releaseYear: 1965,
},
{
id: 5,
name: 'A Coisa',
genre: 'Terror',
author: {
name: 'Stephen King',
birthYear: 1947,
},
releaseYear: 1986,
},
{
id: 6,
name: 'O Chamado de Cthulhu',
genre: 'Terror',
author: {
name: 'H. P. Lovecraft',
birthYear: 1890,
},
releaseYear: 1928,
},
];

const expectedResult = 43;

function averageAge() {
// escreva seu código aqui
//quis fazer tudo com reduce kkkk, mas o primeiro código que fiz está no comentario ali em baixo
//convenhamos, o ultimo código esta ligeiramente mais bonito...
const sumIdades = books.reduce((acc, book) => acc + (book.releaseYear - book.author.birthYear)/books.length, 0)
return parseInt(sumIdades);
}

assert.strictEqual(averageAge(), expectedResult);
// const sumIdades = books.reduce((acc, book) => acc + (book.releaseYear - book.author.birthYear), 0)
// return sumIdades/books.length;
82 changes: 82 additions & 0 deletions bloco_8/dia_4/Exercicio_1/exercise4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const assert = require('assert');

const books = [
{
id: 1,
name: 'As Crônicas de Gelo e Fogo',
genre: 'Fantasia',
author: {
name: 'George R. R. Martin',
birthYear: 1948,
},
releaseYear: 1991,
},
{
id: 2,
name: 'O Senhor dos Anéis',
genre: 'Fantasia',
author: {
name: 'J. R. R. Tolkien',
birthYear: 1892,
},
releaseYear: 1954,
},
{
id: 3,
name: 'Fundação',
genre: 'Ficção Científica',
author: {
name: 'Isaac Asimov',
birthYear: 1920,
},
releaseYear: 1951,
},
{
id: 4,
name: 'Duna',
author: {
name: 'Frank Herbert',
birthYear: 1920,
},
releaseYear: 1965,
},
{
id: 5,
name: 'A Coisa',
genre: 'Terror',
author: {
name: 'Stephen King',
birthYear: 1947,
},
releaseYear: 1986,
},
{
id: 6,
name: 'O Chamado de Cthulhu',
genre: 'Terror',
author: {
name: 'H. P. Lovecraft',
birthYear: 1890,
},
releaseYear: 1928,
},
];

const expectedResult = {
author: {
birthYear: 1948,
name: 'George R. R. Martin',
},
genre: 'Fantasia',
id: 1,
name: 'As Crônicas de Gelo e Fogo',
releaseYear: 1991,
};

function longestNamedBook() {
// escreva seu código aqui
return books.reduce((acc, book) => acc.name.length > book.name.length ? acc : book)
}
console.log(longestNamedBook())

assert.deepStrictEqual(longestNamedBook(), expectedResult);
20 changes: 20 additions & 0 deletions bloco_8/dia_4/Exercicio_1/exercise5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

const assert = require('assert');

const names = [
'Aanemarie', 'Adervandes', 'Akifusa',
'Abegildo', 'Adicellia', 'Aladonata',
'Abeladerco', 'Adieidy', 'Alarucha',
];

const countLetters = (acc, nomeArray) => nomeArray === 'a' ? acc += 1 : acc ;

function containsA() {
// escreva seu código aqui
return names.reduce((acc, nome) => {
const arrayNome = nome.toLowerCase().split('')
return acc += arrayNome.reduce(countLetters, 0)
}, 0)
}

assert.deepStrictEqual(containsA(), 20);
21 changes: 21 additions & 0 deletions bloco_8/dia_4/Exercicio_1/exercise6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const assert = require('assert');

const students = ['Pedro Henrique', 'Miguel', 'Maria Clara'];
const grades = [[9, 8, 10, 7, 5], [10, 9, 9, 10, 8], [10, 7, 10, 8, 9]];

function studentAverage() {
// escreva seu código aqui
const gradesLength = grades[0].length;
const sumArray = grades.map(element => element.reduce((acc, item) => acc + item, 0))
const media = sumArray.map(element => element/gradesLength)
return students.map((stundent, index) => {
return { name: stundent, average: media[index] }
})
}
const expected = [
{ name: 'Pedro Henrique', average: 7.8 },
{ name: 'Miguel', average: 9.2 },
{ name: 'Maria Clara', average: 8.8 },
];

assert.deepStrictEqual(studentAverage(), expected);
79 changes: 79 additions & 0 deletions bloco_8/dia_4/pratica_1/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const estudantes = [
{
nome: 'Jorge',
sobrenome: 'Silva',
idade: 14,
turno: 'Manhã',
materias: [
{ name: 'Matemática', nota: 67 },
{ name: 'Português', nota: 79 },
{ name: 'Química', nota: 70 },
{ name: 'Biologia', nota: 65 },
],
},
{
nome: 'Mario',
sobrenome: 'Ferreira',
idade: 15,
turno: 'Tarde',
materias: [
{ name: 'Matemática', nota: '59' },
{ name: 'Português', nota: '80' },
{ name: 'Química', nota: '78' },
{ name: 'Biologia', nota: '92' },
],
},
{
nome: 'Jorge',
sobrenome: 'Santos',
idade: 15,
turno: 'Manhã',
materias: [
{ name: 'Matemática', nota: '76' },
{ name: 'Português', nota: '90' },
{ name: 'Química', nota: '70' },
{ name: 'Biologia', nota: '80' },
],
},
{
nome: 'Maria',
sobrenome: 'Silveira',
idade: 14,
turno: 'Manhã',
materias: [
{ name: 'Matemática', nota: '91' },
{ name: 'Português', nota: '85' },
{ name: 'Química', nota: '92' },
{ name: 'Biologia', nota: '90' },
],
},
{
nome: 'Natalia',
sobrenome: 'Castro',
idade: 14,
turno: 'Manhã',
materias: [
{ name: 'Matemática', nota: '70' },
{ name: 'Português', nota: '70' },
{ name: 'Química', nota: '60' },
{ name: 'Biologia', nota: '50' },
],
},
{
nome: 'Wilson',
sobrenome: 'Martins',
idade: 14,
turno: 'Manhã',
materias: [
{ name: 'Matemática', nota: '80' },
{ name: 'Português', nota: '82' },
{ name: 'Química', nota: '79' },
{ name: 'Biologia', nota: '75' },
],
},
];
const melhoresNotas = (init, item) => init.nota > item.nota ? init : item;
const notasAltas = estudantes.map((elemento) => elemento.materias.reduce(melhoresNotas))
const resultadoFinal = estudantes.reduce((acc, estudante, index) => {
return acc.concat({ nome: estudante.nome, materia: notasAltas[index].name})
}, [])