pull/848/merge
Gideon Buba 9 months ago committed by GitHub
commit 857f57c4fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,2 +1,300 @@
console.log(countries)
alert('Open the console and check if the countries has been loaded')
// alert('Open the console and check if the countries has been loaded')
// Day 7 exercises
// Exercise 1 & 2
const fullName = (firstName, secondName) => {
return `My full name is ${firstName} ${secondName}`
};
const name = fullName('Gideon', 'Buba')
console.log(name);
// Exercise 3
function addNum(x, y) {
let sum = x + y;
return sum;
}
console.log(addNum(20, 20))
// Exercise 4
function areaOfRectangle(length, width) {
const rectangleArea = length * width;
return rectangleArea;
}
console.log(areaOfRectangle(50, 30))
// Exercise 5
function perimeterOfRectangle(length, width) {
let perimeter = 2 * (length + width)
return perimeter
}
console.log(perimeterOfRectangle(50, 30))
// Exercise 6
function volumeOfRectPrism(length, width, height) {
let volume = length * width * height;
return volume;
}
console.log(volumeOfRectPrism(70, 30, 40))
// Exercise 7
function areaOfCircle(r) {
let π = Math.PI;
let area = π * r * r;
return area;
}
console.log(areaOfCircle(30))
// Exercise 8
function circumOfCircle(r) {
let π = Math.PI;
let circumference = 2*π*r ;
return circumference
}
console.log(circumOfCircle(10))
// Exercise 9
function calculateDensity(mass, volume) {
let density = mass / volume;
return density;
}
console.log(calculateDensity(20, 30))
// Exercise 10
function calculateSpeed (distance, time) {
let speed = distance / time;
return speed;
}
console.log(calculateSpeed(50, 60))
// Exercise 11
function calculateWeight (mass) {
const gravity = 9.8
const weight = mass * gravity;
return weight;
}
console.log(calculateWeight(60))
// Exercise 12
function convertCelsiusToFahrenheit (celcius) {
let conversion = (celcius * 9 / 5) + 32;
return conversion;
}
console.log(convertCelsiusToFahrenheit(50))
// Exercise 13
function bmi (weightInKg, heightInMeterSquare) {
let findBmi = weightInKg / (heightInMeterSquare * heightInMeterSquare);
if (findBmi < 18.5) {
return 'You are underweight'
} else if (findBmi >= 18.8 && findBmi < 24.9) {
return "Your BMI is normal"
} else if (findBmi === 25 && findBmi < 29.9) {
return `You have a slightly overweight`
} else if (findBmi >= 30) {
return `You are obese`;
} else {
return 'Invalid Input'
}
}
console.log(bmi(72, 1.83))
// Exercise 14
function checkSeason(month) {
const autumn = ['september', 'october', 'november']
const winter = ['december', 'january', 'february']
const spring = ['march', 'april', 'may']
const summer = ['june', 'july', 'august']
const lowerCaseMonth = month.toLowerCase();
if (autumn.includes(lowerCaseMonth)) {
return `${month} is autumn`
} else if (winter.includes(lowerCaseMonth)) {
return `${month} is winter`
} else if (spring.includes(lowerCaseMonth)) {
return `${month} is spring`
} else if (summer.includes(lowerCaseMonth)) {
return `${month} is summer`
} else {
return 'Invalid Input'
}
}
console.log(checkSeason('JuNe'))
// Exercise 15
function largestArgument (x, y, z) {
let highestValue = Math.max(x, y, z)
return highestValue;
}
console.log(largestArgument(10, 7, 19))
// Day 7 exercises Level 2
// Exercise 1
function solveLinEquation(a, b, c, x, y) {
const linearEquation = a * x + b * y + c;
return linearEquation;
}
const result = solveLinEquation(2, 3, 4, 5, 6);
console.log(result); // Example input: a = 2, b = 3, c = 4, x = 5, y = 6
// Exercise 2
function solveQuadratic(a, b, c) {
if (a === undefined) {
return { 0: 0 };
}
const discriminant = b ** 2 - 4 * a * c;
if (discriminant > 0) {
const x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
const x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
return { x1, x2 };
} else if (discriminant === 0) {
const x = -b / (2 * a);
return { x };
} else {
return {};
}
}
console.log(solveQuadratic()); // {0}
console.log(solveQuadratic(1, 4, 4)); // {-2}
console.log(solveQuadratic(1, -1, -2)); // {2, -1}
console.log(solveQuadratic(1, 7, 12)); // {-3, -4}
console.log(solveQuadratic(1, 0, -4)); // {2, -2}
console.log(solveQuadratic(1, -1, 0)); // {1, 0}
// Exercise 3
function printArray(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
const myArray = [1, 2, 3, 4, 5];
printArray(myArray);
// Exercise 4
function showDateTime () {
let date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return `${day}/${month}/${year}`;
}
showDateTime()
// Exercise 5
function swapValues(x, y) {
let temp = x;
x = y;
y = temp;
return { x, y };
}
console.log(swapValues(3, 4));
console.log(swapValues(4, 5));
// Exercise 6
function reverseArray(arr) {
let reversed = [];
for (let i = arr.length - 1; i >= 0; i--) {
reversed.push(arr[i]);
}
return reversed;
}
console.log(reverseArray([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]
console.log(reverseArray(['A', 'B', 'C'])); // Output: ['C', 'B', 'A']
// Exercise 7
function capitalizeArray(arr) {
let upperCase = [];
for (let i = 0; i < arr.length; i++) {
let capitalizeElement = arr[i].toUpperCase();
upperCase.push(capitalizeElement);
}
return upperCase;
}
console.log(capitalizeArray(['a', 'b', 'c', 'd']))
// Exercise 8
function addItem(item) {
let itemArr = [];
itemArr.push(item);
return itemArr;
}
console.log(addItem('Egusi')); // Output: ['Egusi']
// Exercise 9
function removeItem(arr, index) {
if (index >= 0 && index < arr.length) {
arr.splice(index, 1);
}
return arr;
}
const originalArray = ['apple', 'banana', 'cherry', 'date'];
const modifiedArray = removeItem(originalArray, 1);
console.log(modifiedArray);
// Exercise 10
function sumOfNumbers (x, y) {
let sum = 0;
if (x < y) {
for (let i = x; i <= y; i++) {
sum += i;
}
}
return sum
}
console.log(sumOfNumbers(1, 5))
// Exercise 11
function sumOfOdds (x, y) {
let sum = 0;
if (x % 2 === 0 || y % 2 === 0) {
for (let i = x; i <= y; i++) {
sum += i;
}
return sum;
}
}
console.log(sumOfOdds)

@ -1,2 +1,154 @@
console.log(countries)
alert('Open the console and check if the countries has been loaded')
const users = {
Alex: {
email: 'alex@alex.com',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 20,
isLoggedIn: false,
points: 30
},
Asab: {
email: 'asab@asab.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'],
age: 25,
isLoggedIn: false,
points: 50
},
Brook: {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
},
Daniel: {
email: 'daniel@alex.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
age: 20,
isLoggedIn: false,
points: 40
},
John: {
email: 'john@john.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
age: 20,
isLoggedIn: true,
points: 50
},
Thomas: {
email: 'thomas@thomas.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React'],
age: 20,
isLoggedIn: false,
points: 40
},
Paul: {
email: 'paul@paul.com',
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
age: 20,
isLoggedIn: false,
points: 40
}
}
//alert('Open the console and check if the countries has been loaded')
// Day 8
// Exercise 1
const dog = {};
// Exercise 2
console.log(dog);
// Exercise 3
dog.name = 'Bingo';
dog.legs = 4;
dog.color = 'Brown';
dog.age = 10;
dog.bark = function () {
return 'woof woof'
}
// Exercise 4
console.log(dog.name);
console.log(dog.legs);
console.log(dog.color);
console.log(dog.age);
console.log(dog.bark());
// Exercise 5
dog.breed = 'Bulldog';
dog.getDogInfo = function () {
return `${this.name} is a ${this.color} ${this.breed}, it has ${this.legs} legs`;
}
console.log(dog.getDogInfo);
// Exercise Level 2
// Exercise 1
let maxSkills = 0;
let skillFullPerson = '';
for (const user in users) {
if (users[user].skills.length > maxSkills) {
maxSkills = users[user].skills.length;
skillFullPerson = user;
}
}
console.log(skillFullPerson)
// Exercise 2
let loggedInCount = 0;
let highPointsCount = 0;
for (const user in users) {
if (users[user].isLoggedIn) {
loggedInCount++;
}
if (users[user].points >= 50) {
highPointsCount++;
}
}
console.log('Logged In Users:', loggedInCount);
console.log('Users with >= 50 Points:', highPointsCount);
// Exercise 3
let mernDevelopers = [];
for (const user in users) {
if (users[user].skills.includes('MongoDB') && users[user].skills.includes('Express') && users[user].skills.includes('React') && users[user].skills.includes('Node')) {
mernDevelopers.push(user);
}
}
console.log(`MERN developers are: ${mernDevelopers}`)
// Exercise 4
const myName = 'Gideon';
users[myName] = {
email: 'bubaambore@gmail.com.com',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 21,
isLoggedIn: false,
points: 30
}
// Exercise 5
const keys = Object.keys(users);
console.log('Keys:', keys);
// Exercise 6
const values = Object.values(users);
console.log('Values:', values)
// Exercise 7
const countries = {
USA: {
capital: 'Washington, D.C.',
population: 331002651,
languages: ['English']
}
}

@ -1,2 +1,116 @@
console.log(countries)
alert('Open the console and check if the countries has been loaded')
//console.log(countries)
// alert('Open the console and check if the countries has been loaded')
const countries = ['Finland', 'Sweden', 'Denmark', 'Norway', 'IceLand']
const names = ['Asabeneh', 'Mathias', 'Elias', 'Brook']
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const products = [
{ product: 'banana', price: 3 },
{ product: 'mango', price: 6 },
{ product: 'potato', price: ' ' },
{ product: 'avocado', price: 8 },
{ product: 'coffee', price: 10 },
{ product: 'tea', price: '' },
]
// Exercise level 1
// Explain the difference betwenn forEach, Map, and reduce
// Exercise 1
/*
The forEach method is a higher order function that is used to loop through elements of an array and apply the provided function to each element.
The map function is used to transform elements of an array, it creates a new array by applying the provided function to each element of the original array.
The reduce function is used to process elements of an array an produce a single result.
*/
// Exercise 2
// Exercise 3
countries.forEach((country) => console.log(country))
// Exercise 4
names.forEach((name) => console.log(name))
// Exercise 5
numbers.forEach((number) => console.log(number))
// Exercise 6
const upperCaseCountries = countries.map((country) => country.toUpperCase());
// Exercise 7
const countriesLength = countries.map((country) => country.length);
// Exercise 8
const square = numbers.map((number) => number * number)
// Exercise 9
const upperCaseNames = names.map((name) => name.toUpperCase())
// Exercise 10
const prices = products.map(product => product.price);
// Exercise 11
const countriesWithLand = countries.filter((country) => country.includes('land'))
// Exercise 12
const countriesWithSixLetters = countries.filter((country) => country.length === 6);
// Exercise 13
const countriesWithSixLettersandMore = countries.filter((country) => country.length >= 6);
// Exercise 14
const countriesStartingWithE = countries.filter((country) => country.startsWith('E'));
// Exercise 15
const pricesWithValues = products.filter((price) => typeof products.price === 'number' && !isNaN(price));
// Exercise 16
function getStringList(array) {
const nonStringElements = array.filter((element) => typeof element !== 'string');
return nonStringElements
}
// Exercise 17
const sumNumbers = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sumNumbers)
// Exercise 18
const europeanCountries = countries.reduce((country) )
// Exercise 19
/*
- `some`: Returns `true` if at least one element in the array satisfies the provided condition.
- `every`: Returns `true` only if all elements in the array satisfy the provided condition.
*/
// Exercise 20
const namesGreaterThanSeven = names.some((name) => name > 7);
// Exercise 21
const everyCountryWithLand = countries.every((country) => country.includes('land'))
//Exercise 22
/*
find: Returns the first element in the array that satisfies the provided condition, or undefined if no element is found.
findIndex: Returns the index of the first element in the array that satisfies the provided condition, or -1 if no element is found.
*/
// Exercise 23
const countryWithSixLetters = countries.find((country) => country.length === 6);
// Exercise 24
const indexOfcountryWithSixLetters = countries.findIndex((country) => country.length === 6);
// Exercise 25
const indexOfNorway = countries.findIndex((country) => country === 'Norway');
// Exercise 26
const indexOfRussia = countries.findIndex((country) => country === 'Russia');
// Exercise level 2
// Exercise 1

Loading…
Cancel
Save