add solutions to day01

pull/344/head
catsAndIvy 2 years ago
parent 0db687a7f5
commit 89dca53a25

@ -10,19 +10,62 @@ const webTechs = [
'MongoDB', 'MongoDB',
] ]
// Declare an empty array;
const newArr = Array()
// Declare an array with more than 5 number of elements
const newArray = Array(6).fill('cats'); const newArray = Array(6).fill('cats');
newArray.push(7)
// Find the length of your array
newArray.length
// Get the first item, the middle item and the last item of the array
const firstItem = newArray[0]
const lastItem = newArray[newArray.length-1]
const middleItem = newArray[3]
// Declare an array called mixedDataTypes, put different data types in the array and find the length of the array. The array size should be greater than 5
const mixedDataTypes = Array(6).fill('cats')
mixedDataTypes.push(7)
mixedDataTypes.length
// Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon
const itCompanies = Array('Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon') const itCompanies = Array('Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon')
// Print the array using console.log()
console.log(itCompanies)
// Print the number of companies in the array
itCompanies.length
// Print the first company, middle and last company
const firstCompany = itCompanies[0]
const lastCompany = itCompanies[itCompanies.length-1]
const middleCompany = itCompanies[3]
console.log(lastCompany)
// Print out each company
for (let i = 0; i<itCompanies.length; i++) { for (let i = 0; i<itCompanies.length; i++) {
return (itCompanies[i].toLowerCase()) console.log(itCompanies[i])
} }
// Change each company name to uppercase one by one and print them out
for (let i = 0; i<itCompanies.length; i++) {
itCompanies[i].toUpperCase()
console.log(itCompanies[i])
}
// Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies.
const store = itCompanies.pop() const store = itCompanies.pop()
const string = itCompanies.join(', ') const string = itCompanies.join(', ')
const sentence = string + " and " + store + " are big IT Companies." const sentence = string + " and " + store + " are big IT Companies."
// return sentence console.log(sentence)
itCompanies.push(store)
// Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found.
for (let i = 0; i < itCompanies.length; i++) { for (let i = 0; i < itCompanies.length; i++) {
let index = itCompanies.indexOf(itCompanies[i]) let index = itCompanies.indexOf(itCompanies[i])
@ -31,25 +74,49 @@ for (let i = 0; i < itCompanies.length; i++) {
: console.log('This company does not exist in the array') : console.log('This company does not exist in the array')
} }
// Filter out companies which have more than one 'o' without the filter method
for (let i = 0; i < itCompanies.length; i++) { for (let i = 0; i < itCompanies.length; i++) {
if (itCompanies[i].includes('o')) { if (itCompanies[i].includes('o')) {
let matcher = itCompanies[i].match(/o/g).length let matcher = itCompanies[i].match(/o/g).length
if (matcher > 1) { if (matcher > 1) {
return itCompanies[i] console.log(itCompanies[i])
} }
} }
} }
const sliced = itCompanies.slice(itCompanies.length-3, itCompanies.length) // Sort the array using sort() method
itCompanies.sort()
// Reverse the array using reverse() method
itCompanies.reverse()
// Slice out the first 3 companies from the array
const slicedFirstThree = itCompanies.slice(0, 3)
// Slice out the last 3 companies from the array
const slicedLastThree = itCompanies.slice(itCompanies.length-3, itCompanies.length)
// Slice out the middle IT company or companies from the array
const middle = itCompanies[Math.floor((itCompanies.length)/2)] const middle = itCompanies[Math.floor((itCompanies.length)/2)]
const middleIndex = Math.floor((itCompanies.length)/2)
itCompanies.splice(middleIndex,1) // Remove the first IT company from the array
const removedFirst = itCompanies.shift()
itCompanies.length = 0 itCompanies.unshift('Oracle')
// Remove the middle IT company or companies from the array
const middleIndex = Math.floor((itCompanies.length)/2)
const removedMiddle = itCompanies.splice(middleIndex,1)
itCompanies.splice(middleIndex, 0, ...removedMiddle);
// Remove the last IT company from the array
const removedLast = itCompanies.pop()
// Remove all IT companies
itCompanies.length = 0

@ -1,8 +1,13 @@
// Exercise 1 - Level 2 // Exercise 1 - Level 2
// 1. Create a separate countries.js file and store the countries array into this file, create a separate file web_techs.js and store the webTechs array into this file. Access both file in main.js file.
const countries = require('./countries.js'); const countries = require('./countries.js');
const webTechs = require('./ web_techs.js'); const webTechs = require('./ web_techs.js');
// 2. First remove all the punctuations and change the string to array and count the number of words in the array
const str = countries.toString() const str = countries.toString()
const modifiedStr = str.replace(/[^\w]/g, ' ') const modifiedStr = str.replace(/[^\w]/g, ' ')
@ -12,6 +17,12 @@ const newArray = modifiedStr.split(' ')
const numOfWords = newArray.length const numOfWords = newArray.length
// 3. In the following shopping cart add, remove, edit items:
// add 'Meat' in the beginning of your shopping cart if it has not been already added
// add Sugar at the end of you shopping cart if it has not been already added
// remove 'Honey' if you are allergic to honey
// modify Tea to 'Green Tea'
const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'] const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey']
if (!shoppingCart.includes('Meat')) { if (!shoppingCart.includes('Meat')) {
@ -25,6 +36,7 @@ if (!shoppingCart.includes('Sugar')) {
shoppingCart[3] = 'Green Tea' shoppingCart[3] = 'Green Tea'
// 4. In countries array check if 'Ethiopia' exists in the array if it exists print 'ETHIOPIA'. If it does not exist add to the countries list.
if (!countries.includes('Ethiopia')) { if (!countries.includes('Ethiopia')) {
countries.push('Ethiopia') countries.push('Ethiopia')
@ -33,6 +45,7 @@ if (!countries.includes('Ethiopia')) {
} }
// 5. In the webTechs array check if Sass exists in the array and if it exists print 'Sass is a CSS preprocess'. If it does not exist add Sass to the array and print the array.
if (!webTechs.includes('Sass')) { if (!webTechs.includes('Sass')) {
webTechs.push('Sass') webTechs.push('Sass')
@ -41,6 +54,9 @@ if (!webTechs.includes('Sass')) {
console.log('Sass is a CSS preprocess') console.log('Sass is a CSS preprocess')
} }
// 6. Concatenate the following two variables and store it in a fullStack variable.
const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux'] const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']
const backEnd = ['Node', 'Express', 'MongoDB'] const backEnd = ['Node', 'Express', 'MongoDB']

@ -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)

@ -1,5 +1,7 @@
// Exercise 2 - Level 1 // Exercise 2 - Level 1
// 1. If user is 18 or older , give feedback:'You are old enough to drive' but if not 18 give another feedback stating to wait for the number of years he needs to turn 18.
let age = 34 let age = 34
if (age <18) { if (age <18) {
@ -10,6 +12,8 @@ if (age <18) {
} }
// 2. Compare the values of myAge and yourAge using if … else. Based on the comparison and log the result to console stating who is older (me or you).
if (age > 29) { if (age > 29) {
let years = age - 29 let years = age - 29
console.log(`You are ${years} years older than me.`) console.log(`You are ${years} years older than me.`)
@ -21,6 +25,8 @@ if (age > 29) {
} }
// 3. If a is greater than b return 'a is greater than b' else 'a is less than b'.
let a = 2 let a = 2
let b = 2 let b = 2
@ -33,6 +39,8 @@ if (a > b) {
} }
//4. Even numbers are divisible by 2 and the remainder is zero. How do you check, if a number is even or not using JavaScript?
let number = 10 let number = 10
if (number%2 === 0) { if (number%2 === 0) {

@ -1,6 +1,12 @@
// Exercise 2 - Level 2 // Exercise 2 - Level 2
let score = 75; // 1. Write a code which can give grades to students according to theirs scores:
// 80-100, A
// 70-89, B
// 60-69, C
// 50-59, D
// 0-49, F
// let score = 75;
if (score >= 90 && score <= 100) { if (score >= 90 && score <= 100) {
console.log('A'); console.log('A');
@ -17,6 +23,12 @@ if (score >= 90 && score <= 100) {
} }
// 2. Check if the season is Autumn, Winter, Spring or Summer. If the user input is :
// September, October or November, the season is Autumn.
// December, January or February, the season is Winter.
// March, April or May, the season is Spring
// June, July or August, the season is Summer
let month = 'September' let month = 'September'
if (month === 'September' || month === 'October' || month === 'November') { if (month === 'September' || month === 'October' || month === 'November') {
@ -26,11 +38,13 @@ if (month === 'September' || month === 'October' || month === 'November') {
} else if (month === 'March' || month === 'April' || month === 'May') { } else if (month === 'March' || month === 'April' || month === 'May') {
console.log('The season is Spring') console.log('The season is Spring')
} else if (month === 'June' || month === 'July' || month === 'August') { } else if (month === 'June' || month === 'July' || month === 'August') {
console.log('The season is Summer') } console.log('The season is Summer')
}
let day = 'FriDAY' // 3. Check if a day is weekend day or a working day. Your script will take day as an input.
let day = 'FriDAY'
let lowered = day.toLowerCase() let lowered = day.toLowerCase()
switch(lowered){ switch(lowered){

@ -1,5 +1,7 @@
// Exercise 2 - Level 3 // Exercise 2 - Level 3
// 1. Write a program which tells the number of days in a month. Then, consider leap year.
const currentDate = new Date(); const currentDate = new Date();
const currentYear = currentDate.getFullYear(); const currentYear = currentDate.getFullYear();

@ -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…
Cancel
Save