parent
0db687a7f5
commit
89dca53a25
@ -1,29 +1,37 @@
|
|||||||
// Exercise 1 - Level 3
|
// Exercise 1 - Level 3
|
||||||
|
|
||||||
const countries = require('./countries.js');
|
// 1. The following is an array of 10 students ages:
|
||||||
const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]
|
const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]
|
||||||
|
|
||||||
|
// - Sort the array and find the min and max age
|
||||||
const sortedArray = ages.sort()
|
const sortedArray = ages.sort()
|
||||||
|
|
||||||
const minAge = sortedArray[0]
|
const minAge = sortedArray[0]
|
||||||
const maxAge = sortedArray[sortedArray.length-1]
|
const maxAge = sortedArray[sortedArray.length-1]
|
||||||
|
|
||||||
|
// - Find the median age(one middle item or two middle items divided by two)
|
||||||
const medianAge = (sortedArray[4] + sortedArray[5])/2
|
const medianAge = (sortedArray[4] + sortedArray[5])/2
|
||||||
|
|
||||||
|
// - Find the average age(all items divided by number of items)
|
||||||
const averageAge = (sortedArray.reduce((number, a) => number + a, 0))/sortedArray.length
|
const averageAge = (sortedArray.reduce((number, a) => number + a, 0))/sortedArray.length
|
||||||
|
|
||||||
|
// - Find the range of the ages(max minus min)
|
||||||
const rangeAge = maxAge - minAge
|
const rangeAge = maxAge - minAge
|
||||||
|
|
||||||
|
// - Compare the value of (min - average) and (max - average)
|
||||||
const compareAge = (maxAge - averageAge)-(minAge - averageAge)
|
const compareAge = (maxAge - averageAge)-(minAge - averageAge)
|
||||||
|
|
||||||
|
|
||||||
|
// 2. The following is a countries array:
|
||||||
|
const countries = require('./countries.js');
|
||||||
|
|
||||||
|
// Slice the first ten countries from the countries array
|
||||||
const slicedCountries = countries.slice(0,10)
|
const slicedCountries = countries.slice(0,10)
|
||||||
|
|
||||||
|
// Find the middle country(ies) in the countries array
|
||||||
const middleCountry = countries[5]
|
const middleCountry = countries[5]
|
||||||
|
|
||||||
const firstHalf = countries.slice(0,6)
|
// Divide the countries array into two equal arrays if it is even. If countries array is not even , one more country for the first half.
|
||||||
|
|
||||||
|
const firstHalf = countries.slice(0,6)
|
||||||
const secondHalf = countries.slice(6,11)
|
const secondHalf = countries.slice(6,11)
|
||||||
|
|
||||||
|
@ -0,0 +1,63 @@
|
|||||||
|
// Exercise 4 - Level 1
|
||||||
|
|
||||||
|
// 1. Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name.
|
||||||
|
|
||||||
|
const fullName = function (firstName, lastName) {
|
||||||
|
console.log(firstName + " " + lastName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Declare a function addNumbers and it takes two two parameters and it returns sum.
|
||||||
|
|
||||||
|
const addNumbers = function (a, b) {
|
||||||
|
console.log(a + b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle
|
||||||
|
|
||||||
|
const areaOfCircle = function (radius) {
|
||||||
|
const circleArea = Math.PI * radius * radius
|
||||||
|
console.log(`The area of the circle is ${circleArea}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 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 convertCelciusToFahrenheit = function (celsius) {
|
||||||
|
const fahrenheit = (celsius * 9/5) + 32
|
||||||
|
console.log(`${celsius} degrees Celsius is the same as ${fahrenheit} degrees Fahrenheit`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 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.
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
|
||||||
|
const checkBMI = function (weight, height) {
|
||||||
|
const bmi = weight / (height * height)
|
||||||
|
if (bmi >= 30) {
|
||||||
|
console.log(`Your BMI is ${bmi} which is 'obese'.`);
|
||||||
|
} else if (bmi >= 25 && bmi <= 29.9) {
|
||||||
|
console.log(`Your BMI is ${bmi} which is 'overweight'.`);
|
||||||
|
} else if (bmi >= 18.5 && bmi <= 24.9) {
|
||||||
|
console.log(`Your BMI is ${bmi} which is 'normal weight'.`);
|
||||||
|
} else if (bmi < 18.5) {
|
||||||
|
console.log(`Your BMI is ${bmi} which is 'underweight'.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 6. Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer.
|
||||||
|
|
||||||
|
const checkSeason = function (month) {
|
||||||
|
if (month === 'September' || month === 'October' || month === 'November') {
|
||||||
|
console.log('The season is Autumn')
|
||||||
|
} else if (month === 'December' || month === 'January' || month === 'February') {
|
||||||
|
console.log('The season is Winter')
|
||||||
|
} else if (month === 'March' || month === 'April' || month === 'May') {
|
||||||
|
console.log('The season is Spring')
|
||||||
|
} else if (month === 'June' || month === 'July' || month === 'August') {
|
||||||
|
console.log('The season is Summer') }
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
// Exercise 4 - Level 2
|
@ -1,22 +0,0 @@
|
|||||||
// Exercise 4 - Level 1
|
|
||||||
|
|
||||||
// 1. Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 2. Declare a function addNumbers and it takes two two parameters and it returns sum.
|
|
||||||
|
|
||||||
// 3. Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle
|
|
||||||
|
|
||||||
// 4. 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.
|
|
||||||
|
|
||||||
// 5. 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.
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
// 6. Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer.
|
|
Loading…
Reference in new issue