diff --git a/solutions/day-01/.DS_Store b/solutions/day-01/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/solutions/day-01/.DS_Store differ diff --git a/solutions/day-01/conditionals-1.js b/solutions/day-01/conditionals-1.js new file mode 100644 index 0000000..f8aa953 --- /dev/null +++ b/solutions/day-01/conditionals-1.js @@ -0,0 +1,37 @@ + + + //1. Get user input using prompt(“Enter your age:”). 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 = prompt('Enter your age: '); + + if (age >= 18) { + alert('You are old enough to drive.') + } else { + alert(`You are left with ${18 - age} years to drive`) + }; + + + + + //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). Use prompt(“Enter your age:”) to get the age as input. + let myAge = 42; + let yourAge = prompt('Enter your age: '); + + if (yourAge > myAge) { alert(`you are ${yourAge - myAge} years older than me`) } + else if (yourAge < myAge) { alert(`you are ${myAge - yourAge} years younger than me`) } + else { alert('we have the same age!') + }; + + + + //3. If a is greater than b return 'a is greater than b' else 'a is less than b'. Try to implement it in two ways using "if else" and "ternary operator". + let a = 4; + let b = 3; + + a > b ? console.log('a is greater than b') : console.log('a is less than b'); + + if (a>b) {console.log('a is greater than b')} else {console.log('a is less than 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 evenOrOddNumber = prompt('Enter a number: ') + if (evenOrOddNumber % 2 == 0 ) {`${evenOrOddNumber} is an even number`} + else {`${evenOrOddNumber} is an odd number`} diff --git a/solutions/day-01/conditionals-2.js b/solutions/day-01/conditionals-2.js new file mode 100644 index 0000000..2cf7408 --- /dev/null +++ b/solutions/day-01/conditionals-2.js @@ -0,0 +1,32 @@ +//1. Write a code which can give grades to students according to theirs scores: + + //90-100, A + //70-89, B + //60-69, C + //50-59, D + //0-49, F + + + function gradeGenerator() { + let grade = Math.floor(Math.random() * 100) + + switch(true) { + + case (grade > 89) : + document.getElementById("result").innerHTML = "Your grade is: " + grade + "% (A)" + break; + + case (grade > 69 && grade < 90) : document.getElementById("result").innerHTML = "Your grade is: " + grade + "% (B)" + break; + + case (grade > 59 && grade < 70) : document.getElementById("result").innerHTML = "Your grade is: " + grade + "% (C)" + break; + + case (grade > 49 && grade < 60) : document.getElementById("result").innerHTML = "Your grade is: " + grade + "% (D)" + break; + + case (grade < 50) : document.getElementById("result").innerHTML = "Your grade is: " + grade + "% (F)" + break; + + } + } diff --git a/solutions/day-01/conditionals-3.js b/solutions/day-01/conditionals-3.js new file mode 100644 index 0000000..dae2fad --- /dev/null +++ b/solutions/day-01/conditionals-3.js @@ -0,0 +1,26 @@ + + //1. Write a program which tells the number of days in a month. + + //Enter a month: January + //January has 31 days. + + //Enter a month: JANUARY + //January has 31 day + + //Enter a month: February + //February has 28 days. + + //Enter a month: FEbruary + //February has 28 days. + + let month = prompt('Enter a month: '); + let monthCap = month.toUpperCase(); + + if ((monthCap == 'JANUARY') || (monthCap =='MARCH') || (monthCap =='MAY') || (monthCap =='JULY') || (monthCap =='AUGUST') || (monthCap =='OCTOBER') || (monthCap =='DECEMBER')) {alert(`${monthCap} has 31 days`)} + + else if (monthCap == 'FEBRUARY') {alert(`FEBRUARY has 28 days and every four years 29 days`)} + + else if ((monthCap == 'APRIL') || (monthCap =='JUNE') || (monthCap =='SEPTEMBER') || (monthCap =='NOVEMBER')){alert(`${monthCap} has 30 days`)} + + else {alert(`${monthCap} is either not a month or you misspelled + the month`)} diff --git a/solutions/day-01/countries.js b/solutions/day-01/countries.js new file mode 100644 index 0000000..666fbeb --- /dev/null +++ b/solutions/day-01/countries.js @@ -0,0 +1,16 @@ +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', +] + + +//by bagaski diff --git a/solutions/day-01/level1.js b/solutions/day-01/level1.js new file mode 100644 index 0000000..54c7dee --- /dev/null +++ b/solutions/day-01/level1.js @@ -0,0 +1,94 @@ +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', +] + +const webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', +] + +//1. Declare an empty array; +let arr = []; + +//2. Declare an array with more than 5 number of elements +const arrX = ['food', 'drinks', 'games', 'books', 'sports', 'music', 'clothes']; + +//3. Find the length of your array +const arrXlength = arr1.length; + +//4. Get the first item, the middle item and the last item of the array +const arrX1st = arrX[0]; +const arrXmid = arrX[Math.floor(arrX.length/2)] +const arrXend = arrX[arrX.length - 1] + +//5. 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 +let mixedDataTypes = [56, 'year: ', {a: 'height'}, 1978, 'name: ', true, 'Maria']; +const mDtL = mixedDataTypes.length; + +//6. Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon +let itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'] + +//7. Print the array using console.log() +console.log(itCompanies); + +//8. Print the number of companies in the array +console.log(itCompanies.length); + +//9. Print out each company +console.log(itCompanies); + +//10. Change each company name to uppercase one by one and print them out +let itCompaniesStrng = itCompanies.toString(); +let caps = itCompaniesStrng.toUpperCase(); +console.log(caps); + +//11. Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon are big IT companies. +console.log(itCompanies.join(', ') + ' are big IT companies.'); + +//12. Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found +itCompanies.includes('Nike')?"Nike exists":"Company is not found"; + +//13. Filter out companies which have more than one 'o' without the filter method + +//14. Sort the array using sort() method +itCompanies.sort(); + +//15. Reverse the array using reverse() method +itCompanies.reverse(); + +//16. Slice out the first 3 companies from the array +itCompanies.slice(0,3); + +//17. Slice out the last 3 companies from the array +itCompanies.slice(itCompanies.length - 3); + +//18. Slice out the middle IT company or companies from the array + + +//19. Remove the first IT company from the array +itCompanies.shift(); + +//20. Remove the middle IT company or companies from the array + +//21. Remove the last IT company from the array +itCompanies.pop(); + +//22. Remove all IT companies +itCompanies.splice(); + +//by bagaski diff --git a/solutions/day-01/level3.js b/solutions/day-01/level3.js new file mode 100644 index 0000000..ed99be2 --- /dev/null +++ b/solutions/day-01/level3.js @@ -0,0 +1,32 @@ +import { countries } from './countries.js'; + + +//1. The following is an array of 10 students ages: +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24] + +//Sort the array and find the min and max age +const agesOrder = ages.sort(); +const minAge = agesOrder[0]; +const maxAge = agesOrder[agesOrder.length - 1]; + +//Find the median age(one middle item or two middle items divided by two) +const medianAge = agesOrder[agesOrder.length * 0.5]; + +//Find the average age(all items divided by number of items) +const averageAge; //do i need 'for' loop? + +//Find the range of the ages(max minus min) +const rangeOfAges = maxAge - minAge; + +//Compare the value of (min - average) and (max - average), use abs() method + + +//2. Slice the first ten countries from the countries array +let sliceFirstTenCuuntries = countries.slice(0, 11); + +//3. Find the middle country(ies) in the countries array +const middleCountries = countries[countries.length * 0.5]; + +//4. 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 firstCountries = countries.slice(0, countries.length * 0.5 + 1); +const secondCountries = countries.slice(countries.length * 0.5 - 1 , countries.length - 1) diff --git a/solutions/day-01/main.js b/solutions/day-01/main.js new file mode 100644 index 0000000..80949e7 --- /dev/null +++ b/solutions/day-01/main.js @@ -0,0 +1,45 @@ +//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 +import { countries } from './countries.js'; +import { webTechs } from './webTechs.js'; + +//2. First remove all the punctuations and change the string to array and count the number of words in the array +let text = + 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' + +const words = text.split(" "); + +console.log(words); +console.log(words.length); + +//3. In the following shopping cart add, remove, edit items +const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'] + + //add 'Meat' in the beginning of your shopping cart if it has not been already added + shoppingCart.shift(); + shoppingCart.unshift('Meat'); + + //add Sugar at the end of you shopping cart if it has not been already added + shoppingCart.push('Sugar'); + + //remove 'Honey' if you are allergic to honey + shoppingCart.splice(3,1); + + //modify Tea to 'Green Tea' + shoppingCart[2] = '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. +let ETH = countries.includes('Ethiopia') +ETH ? (console.log('ETHIOPIA')) : countries.push('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. +webTechs.includes('Sass') ? console.log('Sass is a CSS preprocess') : webTechs.push('Sass') && console.log(webTechs); + + +//6. Concatenate the following two variables and store it in a fullStack variable. +const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']; +const backEnd = ['Node', 'Express', 'MongoDB']; + +const fullStack = frontEnd.concat(backEnd); +console.log(fullStack); + +//by bagaski diff --git a/solutions/day-01/webTechs.js b/solutions/day-01/webTechs.js new file mode 100644 index 0000000..2025b82 --- /dev/null +++ b/solutions/day-01/webTechs.js @@ -0,0 +1,11 @@ +const webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', +] + +//by bagaski