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

Implementing functions from exercise001, exercise002 and exercise003. #8

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b9573dd
feat: implemeting capitalize function
MarlonTancredo Jul 4, 2023
103af28
feat: adding yarn into the project
MarlonTancredo Jul 4, 2023
27c1f6b
feat: implementing generateInitials function
MarlonTancredo Jul 4, 2023
67639e2
feat: implementing addVAT function
MarlonTancredo Jul 4, 2023
8e3c5b9
feat: implementing getSalePrice function
MarlonTancredo Jul 4, 2023
781214b
feat: implementing getMiddleCharacter
MarlonTancredo Jul 4, 2023
f155010
feat: implementing reverseWord function
MarlonTancredo Jul 4, 2023
c7ba4bc
feat: implementing reverseAllWords
MarlonTancredo Jul 4, 2023
565d60f
feat: implementing countLinuxUsers function
MarlonTancredo Jul 4, 2023
3c59bb5
feat: implementing getMeanScore function
MarlonTancredo Jul 4, 2023
ad85b7b
feat: implementing simpleFizzBuzz function
MarlonTancredo Jul 4, 2023
80a9422
refactor: refactoring all functions
MarlonTancredo Jul 4, 2023
36c513c
feat: implementing getFillings and isFromMAnchester function
MarlonTancredo Jul 5, 2023
a121959
feat: Implementing getBuses function
MarlonTancredo Jul 5, 2023
eea69b8
feat: Implementing countSheep function
MarlonTancredo Jul 5, 2023
f844a1b
feat: Implementing hasMPostCode function
MarlonTancredo Jul 5, 2023
0dbc722
feat: Implementing getSquare function
MarlonTancredo Jul 5, 2023
05d7c9f
feat: Implementing camelCaseWords function
MarlonTancredo Jul 5, 2023
d4d7a29
feat: Implementing getTotalSubejects function
MarlonTancredo Jul 5, 2023
abec486
feat: Implementing checkIngredients function
MarlonTancredo Jul 5, 2023
d6b50bc
feat: Implementing duplicateNumbers function
MarlonTancredo Jul 5, 2023
a9bcae5
feat: implementing findSmallNums, findNamesBeginningWith, findVerbs, …
MarlonTancredo Jul 5, 2023
e826591
feat: Implementing findNextNumber function
MarlonTancredo Jul 7, 2023
7e8c236
feat: Implementing count1sand0s function
MarlonTancredo Jul 7, 2023
8c0549c
feat: Implementing reverseNumber function
MarlonTancredo Jul 7, 2023
6433b82
feat: Implementing sumArrays function
MarlonTancredo Jul 7, 2023
53bd8ec
feat: Implementing arrShift function
MarlonTancredo Jul 7, 2023
90c0017
feat: Implementing findNeedle function
MarlonTancredo Jul 7, 2023
ca021bc
feat: Implementing getWordFrenquecies function
MarlonTancredo Jul 7, 2023
1c89ae3
refactor: refactoring getWordFrenquecies function
MarlonTancredo Jul 7, 2023
41e74c0
feat: implementing sumMultiples function
MarlonTancredo Jul 7, 2023
35526a9
feat: implementing sumMultiples test
MarlonTancredo Jul 7, 2023
0d4feca
feat: implementing isValidDNA function
MarlonTancredo Jul 8, 2023
90bd5b1
feat: implementing isValidDNA test
MarlonTancredo Jul 8, 2023
63929bb
feat: implementing getComplementaryDNA function
MarlonTancredo Jul 8, 2023
df60c9c
feat: implementing getComplementaryDNA test
MarlonTancredo Jul 8, 2023
f0abe7a
feat: implementing isItPrime function
MarlonTancredo Jul 8, 2023
0c8a102
feat: implementing isItPrime test
MarlonTancredo Jul 8, 2023
46541be
feat: implementing createMatrix function
MarlonTancredo Jul 8, 2023
27a142c
feat: implementing createMatrix test
MarlonTancredo Jul 8, 2023
2674a84
feat: implementing areWeCovered function
MarlonTancredo Jul 8, 2023
0f6c0c4
feat: implementing areWeCovered test
MarlonTancredo Jul 8, 2023
e199437
refactor: refactoring areWeCovered function
MarlonTancredo Jul 8, 2023
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
102 changes: 77 additions & 25 deletions challenges/exercise001.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,108 @@
// Note: Be sure to read the corresponding .md file for each exercise, in the docs folder. 📘 👍

export function capitalize(word) {
if (word === undefined) throw new Error('word is required');
// Add your code here!
if (word === undefined) throw new Error("word is required");
// Add your code here!
const wordSplitted = word.split("");
const upperCase = wordSplitted.map((myWord, index) => {
return index === 0 ? myWord.toUpperCase() : myWord;
});
return upperCase.join("");
}

export function generateInitials(firstName, lastName) {
if (firstName === undefined) throw new Error('firstName is required');
if (lastName === undefined) throw new Error('lastName is required');
// Add your code here!
if (firstName === undefined) throw new Error("firstName is required");
if (lastName === undefined) throw new Error("lastName is required");
// Add your code here!
return `${firstName[0].toUpperCase()}.${lastName[0].toUpperCase()}`;
}

export function addVAT(originalPrice, vatRate) {
if (originalPrice === undefined)
throw new Error('originalPrice is requied');
if (vatRate === undefined) throw new Error('vatRate is required');
// Add your code here!
if (originalPrice === undefined) throw new Error("originalPrice is requied");
if (vatRate === undefined) throw new Error("vatRate is required");
// Add your code here!
const sum = (originalPrice / 100) * vatRate;

return Number.isInteger(originalPrice + sum)
? originalPrice + sum
: Number((originalPrice + sum).toFixed(2));
}

export function getSalePrice(originalPrice, reduction) {
if (originalPrice === undefined)
throw new Error('originalPrice is required');
if (reduction === undefined) throw new Error('reduction is required');
// Add your code here!
if (originalPrice === undefined) throw new Error("originalPrice is required");
if (reduction === undefined) throw new Error("reduction is required");
// Add your code here!
const sub = (originalPrice / 100) * reduction;
return Number.isInteger(originalPrice - sub)
? originalPrice - sub
: Number((originalPrice - sub).toFixed(2));
}

export function getMiddleCharacter(str) {
if (str === undefined) throw new Error('str is required');
// Add your code here!
if (str === undefined) throw new Error("str is required");
// Add your code here!
const strSplitted = str.split("");
const evenIndex = strSplitted.length / 2;
const oddIndex = (strSplitted.length - 1) / 2;
return strSplitted.length % 2 === 0
? `${str[evenIndex - 1]}${str[evenIndex]}`
: `${str[oddIndex]}`;
}

export function reverseWord(word) {
if (word === undefined) throw new Error('word is required');
// Add your code here!
if (word === undefined) throw new Error("word is required");
// Add your code here!
const reversedWord = word.split("");
return reversedWord.reverse().join("");
}

export function reverseAllWords(words) {
if (words === undefined) throw new Error('words is required');
// Add your code here!
if (words === undefined) throw new Error("words is required");
// Add your code here!
const reversedWords = words.map((word) => {
return word.split("").reverse().join("");
});
return reversedWords;
}

export function countLinuxUsers(users) {
if (users === undefined) throw new Error('users is required');
// Add your code here!
if (users === undefined) throw new Error("users is required");
// Add your code here!
let counter = 0;
users.map((user) => {
if (user.type === "Linux") {
counter = counter + 1;
}
});
return counter;
}

export function getMeanScore(scores) {
if (scores === undefined) throw new Error('scores is required');
// Add your code here!
if (scores === undefined) throw new Error("scores is required");
// Add your code here!
let sum = 0;
scores.map((score) => {
sum = sum + score;
});

return Number.isInteger(sum / scores.length)
? sum / scores.length
: Number((sum / scores.length).toFixed(2));
}

export function simpleFizzBuzz(n) {
if (n === undefined) throw new Error('n is required');
// Add your code here!
if (n === undefined) throw new Error("n is required");
// Add your code here!
if (n % 3 === 0 && n % 5 !== 0) {
return "fizz";
}
if (n % 5 === 0 && n % 3 !== 0) {
return "buzz";
}
if (n % 3 === 0 && n % 5 == 0) {
return "fizzbuzz";
}
if (n % 3 !== 0 && n % 5 !== 0) {
return n;
}
}
39 changes: 29 additions & 10 deletions challenges/exercise002.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
export function getFillings(sandwich) {
if (sandwich === undefined) throw new Error('ingredients is required');
// Your code here!
if (sandwich === undefined) throw new Error("ingredients is required");
// Your code here!
const { fillings } = sandwich;
return fillings;
}

export function isFromManchester(person) {
if (person === undefined) throw new Error('person is required');
// Your code here!
if (person === undefined) throw new Error("person is required");
// Your code here!
const { city } = person;
return city === "Manchester" ? true : false;
}

export function getBusNumbers(people) {
if (people === undefined) throw new Error('people is required');
// Your code here!
if (people === undefined) throw new Error("people is required");
// Your code here!
const limit = 40;
let buses = Math.ceil(people / limit);
return buses;
}

export function countSheep(arr) {
if (arr === undefined) throw new Error('arr is required');
// Your code here!
if (arr === undefined) throw new Error("arr is required");
// Your code here!
let sheepCounter = 0;
arr.map((animal) => {
if (animal === "sheep") {
sheepCounter = sheepCounter + 1;
}
});
return sheepCounter;
}

export function hasMPostCode(person) {
if (person === undefined) throw new Error('person is required');
// Your code here!
if (person === undefined) throw new Error("person is required");
// Your code here!
const {
address: { city, postCode },
} = person;

return city === "Manchester" && postCode[0] === "M" ? true : false;
}
81 changes: 69 additions & 12 deletions challenges/exercise003.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,83 @@
export function getSquares(nums) {
if (nums === undefined) throw new Error('nums is required');
// Your code here!
if (nums === undefined) throw new Error("nums is required");
// Your code here!
const squares = nums.map((num) => {
return num * num;
});
return squares;
}

export function camelCaseWords(words) {
if (words === undefined) throw new Error('words is required');
// Your code here!
if (words === undefined) throw new Error("words is required");
// Your code here!
const formattedWords = words.map((word, index) => {
return index === 0 ? word : word[0].toUpperCase() + word.slice(1);
});
return formattedWords.join("");
}

export function getTotalSubjects(people) {
if (people === undefined) throw new Error('people is required');
// Your code here!
if (people === undefined) throw new Error("people is required");
// Your code here!
let subjectsCounter = 0;
const subjectsNumber = people.map((people) => {
const { subjects } = people;
return subjects.length;
});

for (let i = 0; i < subjectsNumber.length; i++) {
subjectsCounter = subjectsCounter + subjectsNumber[i];
}

return subjectsCounter;
}

export function checkIngredients(menu, ingredient) {
if (menu === undefined) throw new Error('menu is required');
if (!ingredient) throw new Error('ingredient is required');
// Your code here!
if (menu === undefined) throw new Error("menu is required");
if (!ingredient) throw new Error("ingredient is required");
// Your code here!
let isIncluded = false;
const menus = menu.map((menu) => {
const { ingredients } = menu;
return ingredients;
});

for (let i = 0; i < menus.flat().length; i++) {
if (menus.flat()[i] === ingredient) {
isIncluded = true;
break;
}
}

return isIncluded;
}

export function duplicateNumbers(arr1, arr2) {
if (arr1 === undefined) throw new Error('arr1 is required');
if (arr2 === undefined) throw new Error('arr2 is required');
// Your code here!
if (arr1 === undefined) throw new Error("arr1 is required");
if (arr2 === undefined) throw new Error("arr2 is required");
// Your code here!

const duplicates = [];
if (arr1 > arr2) {
for (let i = 0; i < arr2.length; i++) {
for (let j = 0; j < arr1.length; j++) {
if (arr2[i] === arr1[j]) {
duplicates.push(arr2[i]);
}
}
}
} else {
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
duplicates.push(arr1[i]);
}
}
}
}
const uniqueArray = duplicates.filter((item, index) => {
return duplicates.indexOf(item) == index;
});

return uniqueArray.sort();
}
77 changes: 59 additions & 18 deletions challenges/exercise004.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,82 @@
export function findSmallNums(nums) {
if (!nums) throw new Error('nums is required');
// Your code here
if (!nums) throw new Error("nums is required");
// Your code here
const smaller = nums.filter((num) => {
return num < 1;
});
return smaller;
}

export function findNamesBeginningWith(names, char) {
if (!names) throw new Error('names is required');
if (!char) throw new Error('char is required');
// Your code here
if (!names) throw new Error("names is required");
if (!char) throw new Error("char is required");
// Your code here
const filteredNames = names.filter((name) => {
return name[0] === char;
});
return filteredNames;
}

export function findVerbs(words) {
if (!words) throw new Error('words is required');
// Your code here
if (!words) throw new Error("words is required");
// Your code here
const verbs = words.filter((word) => {
if (word[0] === "t" && word[1] === "o" && word[2] === " ") {
return word;
}
});
return verbs;
}

export function getIntegers(nums) {
if (!nums) throw new Error('nums is required');
// Your code here
if (!nums) throw new Error("nums is required");
// Your code here
const integers = nums.filter((num) => {
return Number.isInteger(num);
});
return integers;
}

export function getCities(users) {
if (!users) throw new Error('users is required');
// Your code here
if (!users) throw new Error("users is required");
// Your code here
const cities = users.map((data) => {
const {
data: {
city: { displayName },
},
} = data;
return displayName;
});
return cities;
}

export function getSquareRoots(nums) {
if (!nums) throw new Error('nums is required');
// Your code here
if (!nums) throw new Error("nums is required");
// Your code here
const squareRoot = nums.map((num) => {
return Number(Math.sqrt(num).toFixed(2));
});
return squareRoot;
}

export function findSentencesContaining(sentences, str) {
if (!sentences) throw new Error('sentences is required');
if (!str) throw new Error('str is required');
// Your code here
if (!sentences) throw new Error("sentences is required");
if (!str) throw new Error("str is required");
// Your code here
const sentence = sentences.filter((sentence) => {
return sentence.toLowerCase().includes(str);
});

return sentence;
}

export function getLongestSides(triangles) {
if (!triangles) throw new Error('triangles is required');
// Your code here
if (!triangles) throw new Error("triangles is required");
// Your code here
const longest = [];
for (let i = 0; i < triangles.length; i++) {
longest.push(Math.max(...triangles[i]));
}
return longest;
}
Loading