Skip to content

Instantly share code, notes, and snippets.

@ssisaias
Last active June 8, 2021 21:57
Show Gist options
  • Save ssisaias/a339e01cc6a0ce06cb1ae041c21d9764 to your computer and use it in GitHub Desktop.
Save ssisaias/a339e01cc6a0ce06cb1ae041c21d9764 to your computer and use it in GitHub Desktop.
simple exercise
// Grade conversion
sistemaNotas = function(nota){
if(nota>100||nota<1) return 'Invalid score';
if(nota<60){
return 'F'
} else if(nota<70) {
return 'D'
} else if(nota<80) {
return 'C'
} else if(nota<90) {
return 'B'
} else if(nota>=90) {
return 'A'
}
}
// Finances
let wallet = {
income: [],
outcome: []
}
fillWallet = function(){
for(let i = 0; i<10;i++){
wallet.income.push(Math.floor(Math.random()*1000)/100)
wallet.outcome.push(Math.floor(Math.random()*1000)/100)
}
}
processBalance = function(){
let position = 0;
for(let income of wallet.income){
position += income
}
for(let outcome of wallet.outcome){
position -= outcome
}
console.log(`Your balance is ${position} and ${(position>0)?'it is positive.':'you should get back to work.'}`)
}
//fillWallet();
//processBalance();
//C to F and F to C - with c or f in the 'temperature' string
convertCtoFandFtoC = function(temperature =''){
if(temperature.toLowerCase().includes('c')){
const value = temperature.toLowerCase().split('c')[0];
return (value * 9/5 + 32)
}
if(temperature.toLowerCase().includes('f')){
const value = temperature.toLowerCase().split('f')[0];
return ((value-32) * 5/9)
}
}
//
// Books by category
const booksByCategory = [
{
category: "Riqueza",
books: [
{
title: "Os segredos da mente milionária",
author: "T. Harv eker"
},
{
title: "O homem mais rico da Babilônia",
author: "George S. Clason"
},
{
title: "Pai rico, pai pobre",
author: "Robert T. Kiyosaki e Sharon L. Lechter"
},
]
},
{
category: "Inteligência Emocional",
books: [
{
title: "Você é Insubstituível",
author: "Augusto Cury"
},
{
title: "Ansiedade - Como enfrentar o mal do século",
author: "Augusto Cury"
},
{
title: "7 hábitos das pessoas altamente eficazes",
author: "Stephen R. Covey"
},
]
}
]
processBooksByCategory = function(authorName) {
let totalCategories = 0;
let totalBooks = 0;
let totalAuthors = 0;
let totalBooksFromAuthor = 0;
booksByCategory.forEach(cat => {
totalCategories++;
cat.books.forEach(() => {
totalBooks++;
});
// count authors
let authors = cat.books.reduce((acc, value) =>
(acc.indexOf(value.author) == -1) ? acc.concat(value.author) : acc, []);
totalAuthors += authors.length;
totalBooksFromAuthor += cat.books.reduce((acc, book) =>
(book.author === authorName ? acc + 1 : acc), 0);
});
console.log(`You have ${totalCategories} categories, ${totalBooks} books total and ${totalAuthors} authors.`);
if (authorName) {
console.log(`Your author ${authorName} has, ${totalBooksFromAuthor} books registered.`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment