parent
b1bf15efa5
commit
0ae13b04ff
@ -1,34 +1,56 @@
|
|||||||
// Create a function called getPersonInfo. The getPersonInfo function takes an object parameter.
|
// Exercises: Level 1
|
||||||
// The structure of the object and the output of the function is given below. Try to use both a regular way and destructuring
|
|
||||||
// and compare the cleanness of the code. If you want to compare your solution with my solution, check this link.
|
|
||||||
|
|
||||||
const person = {
|
// Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name.
|
||||||
firstName: 'Asabeneh',
|
const fullname = (firstname, lastname) => {
|
||||||
lastName: 'Yetayeh',
|
return `${firstname} ${lastname}`
|
||||||
age: 250,
|
}
|
||||||
country: 'Finland',
|
// Declare a function addNumbers and it takes two two parameters and it returns sum.
|
||||||
job: 'Instructor and Developer',
|
const sum = (a, b) =>{
|
||||||
skills: [
|
return a + b
|
||||||
'HTML',
|
}
|
||||||
'CSS',
|
|
||||||
'JavaScript',
|
// Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle
|
||||||
'React',
|
const areaOfCircle = (r,pi=3.14) => {
|
||||||
'Redux',
|
return pi * r * r
|
||||||
'Node',
|
|
||||||
'MongoDB',
|
|
||||||
'Python',
|
|
||||||
'D3.js',
|
|
||||||
],
|
|
||||||
languages: ['Amharic', 'English', 'Suomi(Finnish)'],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getPersonInfo = (person) => {
|
// Temperature in oC can be converted to oF using this formula: oF = (oC x 9/5) + 32. Write a function which convert oC to oF convertCelciusToFahrenheit.
|
||||||
const {firstName, lastName, age, country, job, skills, languages} = person;
|
const temperature = (oC) => {
|
||||||
const {skill1,skill2,skill3,skill4,skill5,skill6,skill7,skill8,skill9} = skills;
|
return (oC * 9/5) + 32
|
||||||
console.log(`${firstName} ${lastName} lives in ${country}. He is ${age} years old. He is an ${job}. He teaches ${skill1}, ${skill2}, ${skill3}, ${skill4}, ${skill5}, ${skill6}, ${skill7}, ${skill8}, ${skill9}. He speaks ${languages[0]}, ${languages[1]} and a little bit of ${languages[2]}`);
|
}
|
||||||
|
|
||||||
|
// Body mass index(BMI) is calculated as follows: bmi = weight in Kg / (height x height) in m2. Write a function which calculates bmi. BMI is used to broadly define different weight groups in adults 20 years old or older.Check if a person is underweight, normal, overweight or obese based the information given below.
|
||||||
|
const BMI = (W,H) => {
|
||||||
|
let bmi= W / (H * H)
|
||||||
|
if (bmi>18.4 || bmi<25){
|
||||||
|
return 'Normal weight'
|
||||||
|
}
|
||||||
|
else if (bmi>25){
|
||||||
|
return 'Overweight'
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 'Underweight'
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// The same groups apply to both men and women.
|
||||||
|
|
||||||
|
// Underweight: BMI is less than 18.5
|
||||||
|
// Normal weight: BMI is 18.5 to 24.9
|
||||||
|
// Overweight: BMI is 25 to 29.9
|
||||||
|
// Obese: BMI is 30 or more
|
||||||
|
|
||||||
getPersonInfo(person)
|
// Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer.
|
||||||
/*
|
const checkSeason = (month) => {
|
||||||
Asabeneh Yetayeh lives in Finland. He is 250 years old. He is an Instructor and Developer. He teaches HTML, CSS, JavaScript, React, Redux, Node, MongoDB, Python and D3.js. He speaks Amharic, English and a little bit of Suomi(Finnish)
|
if (month >= 3 && month <= 5){
|
||||||
*/
|
return 'Spring'
|
||||||
|
}
|
||||||
|
else if (month >= 6 && month <= 8){
|
||||||
|
return 'Summer'
|
||||||
|
}
|
||||||
|
else if (month >= 9 && month <= 11){
|
||||||
|
return 'Autumn'
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 'Winter'
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,152 @@
|
|||||||
|
// Create a function called getPersonInfo. The getPersonInfo function takes an object parameter.
|
||||||
|
// The structure of the object and the output of the function is given below. Try to use both a regular way and destructuring
|
||||||
|
// and compare the cleanness of the code. If you want to compare your solution with my solution, check this link.
|
||||||
|
|
||||||
|
const person = {
|
||||||
|
firstName: 'Asabeneh',
|
||||||
|
lastName: 'Yetayeh',
|
||||||
|
age: 250,
|
||||||
|
country: 'Finland',
|
||||||
|
job: 'Instructor and Developer',
|
||||||
|
skills: [
|
||||||
|
'HTML',
|
||||||
|
'CSS',
|
||||||
|
'JavaScript',
|
||||||
|
'React',
|
||||||
|
'Redux',
|
||||||
|
'Node',
|
||||||
|
'MongoDB',
|
||||||
|
'Python',
|
||||||
|
'D3.js',
|
||||||
|
],
|
||||||
|
languages: ['Amharic', 'English', 'Suomi(Finnish)'],
|
||||||
|
}
|
||||||
|
|
||||||
|
const getPersonInfo = (person) => {
|
||||||
|
const {firstName, lastName, age, country, job, skills, languages} = person;
|
||||||
|
const {skill1,skill2,skill3,skill4,skill5,skill6,skill7,skill8,skill9} = skills;
|
||||||
|
console.log(`${firstName} ${lastName} lives in ${country}. He is ${age} years old. He is an ${job}. He teaches ${skill1}, ${skill2}, ${skill3}, ${skill4}, ${skill5}, ${skill6}, ${skill7}, ${skill8}, ${skill9}. He speaks ${languages[0]}, ${languages[1]} and a little bit of ${languages[2]}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
getPersonInfo(person)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Quadratic equation is calculated as follows: ax2 + bx + c = 0. Write a function which calculates value or values of a quadratic equation, solveQuadEquation.
|
||||||
|
const solveQuadEquation = (a, b, c) => {
|
||||||
|
const discriminant = b * b - 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 "No real roots";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
solveQuadEquation(1, 5, 6); // {x1: -2, x2: -3}
|
||||||
|
|
||||||
|
|
||||||
|
// Declare a function name printArray. It takes array as a parameter and it prints out each value of the array.
|
||||||
|
const printArray = (arr) => {
|
||||||
|
arr.forEach((item) => {
|
||||||
|
console.log(item);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Write a function name showDateTime which shows time in this format: 08/01/2020 04:08 using the Date object.
|
||||||
|
const showDateTime = () => {
|
||||||
|
let now = new Date()
|
||||||
|
let year = now.getFullYear()
|
||||||
|
let month = now.getMonth() + 1
|
||||||
|
let date = now.getDate()
|
||||||
|
let hours = now.getHours()
|
||||||
|
let minutes = now.getMinutes()
|
||||||
|
if (hours < 10) {
|
||||||
|
hours = '0' + hours
|
||||||
|
}
|
||||||
|
else if (hours > 12) {
|
||||||
|
hours -=12
|
||||||
|
}
|
||||||
|
|
||||||
|
let datee = `${year}/${month}/${date}`
|
||||||
|
let Time = `${hours}:${minutes}`
|
||||||
|
return datee + "," + Time
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(showDateTime()) // 2019/10/12 12:46
|
||||||
|
|
||||||
|
// Declare a function name swapValues. This function swaps value of x to y.
|
||||||
|
const swap = (x,y) => {
|
||||||
|
console.log(` before swap : x => ${x}, y => ${y}`)
|
||||||
|
let x =x+y;
|
||||||
|
let y = x-y;
|
||||||
|
x = x-y;
|
||||||
|
|
||||||
|
console.log(` after swap : x => ${x}, y => ${y}`)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare a function name reverseArray. It takes array as a parameter and it returns the reverse of the array (don't use method).
|
||||||
|
const reverse = (arr) => {
|
||||||
|
let reversed =[]
|
||||||
|
for (let i =arr.length-1; i>=0; i--){
|
||||||
|
reversed.push(arr[i])
|
||||||
|
}
|
||||||
|
return reversed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare a function name capitalizeArray. It takes array as a parameter and it returns the - capitalizedarray.
|
||||||
|
const capitalize = (arr) => {
|
||||||
|
let capitalized = []
|
||||||
|
arr.forEach((item) => {
|
||||||
|
capitalized.push(item.toUpperCase())
|
||||||
|
});
|
||||||
|
return capitalized
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare a function name addItem. It takes an item parameter and it returns an array after adding the item
|
||||||
|
const add = (arr, item) => {
|
||||||
|
arr.push(item)
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare a function name removeItem. It takes an index parameter and it returns an array after removing an ite
|
||||||
|
const remove = (arr, index) => {
|
||||||
|
arr.splice(index, 1)
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare a function name evensAndOdds . It takes a positive integer as parameter and it counts number of evens and odds in the number.
|
||||||
|
const evensAndOdds = (num) => {
|
||||||
|
let odds = 0;
|
||||||
|
let evens = 0;
|
||||||
|
for (let i = 0; i <= num; i++) {
|
||||||
|
if (i % 2 === 0) {
|
||||||
|
evens++;
|
||||||
|
} else {
|
||||||
|
odds++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(`The number of odds are ${odds}.`);
|
||||||
|
console.log(`The number of evens are ${evens}.`);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Write a function which takes any number of arguments and return the sum of the arguments
|
||||||
|
const sum = (...args) => {
|
||||||
|
return args.reduce((acc, cur) => acc + cur);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Declare a function name userIdGenerator. When this function is called it generates seven character id. The function return the id.
|
||||||
|
const userIdGenerator = () => {
|
||||||
|
let chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
let id = "";
|
||||||
|
for (let i = 0; i < 7; i++) {
|
||||||
|
let randomIndex = Math.floor(Math.random() * chars.length);
|
||||||
|
id += chars[randomIndex];
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
};
|
@ -1,56 +0,0 @@
|
|||||||
// Exercises: Level 1
|
|
||||||
|
|
||||||
// Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name.
|
|
||||||
const fullname = (firstname, lastname) => {
|
|
||||||
return `${firstname} ${lastname}`
|
|
||||||
}
|
|
||||||
// Declare a function addNumbers and it takes two two parameters and it returns sum.
|
|
||||||
const sum = (a, b) =>{
|
|
||||||
return a + b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle
|
|
||||||
const areaOfCircle = (r,pi=3.14) => {
|
|
||||||
return pi * r * r
|
|
||||||
}
|
|
||||||
|
|
||||||
// Temperature in oC can be converted to oF using this formula: oF = (oC x 9/5) + 32. Write a function which convert oC to oF convertCelciusToFahrenheit.
|
|
||||||
const temperature = (oC) => {
|
|
||||||
return (oC * 9/5) + 32
|
|
||||||
}
|
|
||||||
|
|
||||||
// Body mass index(BMI) is calculated as follows: bmi = weight in Kg / (height x height) in m2. Write a function which calculates bmi. BMI is used to broadly define different weight groups in adults 20 years old or older.Check if a person is underweight, normal, overweight or obese based the information given below.
|
|
||||||
const BMI = (W,H) => {
|
|
||||||
let bmi= W / (H * H)
|
|
||||||
if (bmi>18.4 || bmi<25){
|
|
||||||
return 'Normal weight'
|
|
||||||
}
|
|
||||||
else if (bmi>25){
|
|
||||||
return 'Overweight'
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return 'Underweight'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// The same groups apply to both men and women.
|
|
||||||
|
|
||||||
// Underweight: BMI is less than 18.5
|
|
||||||
// Normal weight: BMI is 18.5 to 24.9
|
|
||||||
// Overweight: BMI is 25 to 29.9
|
|
||||||
// Obese: BMI is 30 or more
|
|
||||||
|
|
||||||
// Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer.
|
|
||||||
const checkSeason = (month) => {
|
|
||||||
if (month >= 3 && month <= 5){
|
|
||||||
return 'Spring'
|
|
||||||
}
|
|
||||||
else if (month >= 6 && month <= 8){
|
|
||||||
return 'Summer'
|
|
||||||
}
|
|
||||||
else if (month >= 9 && month <= 11){
|
|
||||||
return 'Autumn'
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return 'Winter'
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue