From 5ae60e10135f6b24b44ad8c6e09f29bffcb80ffa Mon Sep 17 00:00:00 2001 From: nam dang tran Date: Thu, 4 Feb 2021 17:01:41 +0700 Subject: [PATCH] Array exercise --- .DS_Store | Bin 0 -> 10244 bytes exercise-solutions/.DS_Store | Bin 0 -> 8196 bytes exercise-solutions/day1/.gitignore | 2 + exercise-solutions/day1/1.array.js | 120 +++++++++++++++++++++++++ exercise-solutions/day1/1.countries.js | 13 +++ exercise-solutions/day1/1.web_techs.js | 9 ++ exercise-solutions/day1/main.html | 101 +++++++++++++++++++++ 7 files changed, 245 insertions(+) create mode 100644 .DS_Store create mode 100644 exercise-solutions/.DS_Store create mode 100644 exercise-solutions/day1/.gitignore create mode 100644 exercise-solutions/day1/1.array.js create mode 100644 exercise-solutions/day1/1.countries.js create mode 100644 exercise-solutions/day1/1.web_techs.js create mode 100644 exercise-solutions/day1/main.html diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d5d4eb9cd620bfbb301c917ab6d9506a78fe4327 GIT binary patch literal 10244 zcmeHMUu+ab7@u!j+S#tqZ3VmKPwucl1y5}QlvV?|-nAAe(1yFxmVagMcBLD)yT|R` zwNR_oXiP+nPZ&_+i&0|~9~A#R_@FULVoby)2=ReLjX`}eF&cd^elxRID1S`DgFv#A z%zQK7H{Z<8Z-3wH%^V>F5(T4~kSHO9VrEjk1&b>rW@q`bBn28~1oS5)ODy6MliY=6 zm_h_X1VRKt1VRKt1a3qG@SDvNRfG)75P=YZ5P@j~*!~d3%w#-}lS1;X0}FlxKv|Az zp0JMR0PYhFWIT|QLULCeQ=A?kaE0KA0m2>aQPv%0Jdl$@3U>m+oj@=%1Sb^Wv!h>> zcP9`RGAu&`LIf^HfQ3yhp4i7Q=VzSXU1F00@$p3O5{G!1Q7@OV-3<0WnaSCf=QlN- z2C1xkPDLf9RkUhu)t=O_JDT!!zu+bG{4Un&)&1d2P*1zAJzdu`J1lc_NS&W_9bdN` zGtZqZ!^AGT`z^PyytY_^NH_cy1q?nv$;|5<@%d&Q&xtt?ss?Q97& zxk^#yYU#f8j{bomRV{etcGK27OJ3Tk=C;4Cl7LG4^)v`Hya?IJ= zVdg!QvV44A-QuOoSKYIwZA0Sz?w+Y?wWjt~Wr4C2Ux?#d4_Ib?Q1`N?V_44qzER8Y zhx?0`XJu?N?dT(>to|rZ)z+xhTD`W2stt6Ykg*OFEdL-MYKa!nb{n{kt**faGpPCw z75yXHGD>$bTQYBAX>hgDsI8P$ou)lpnb5?PyJgjr(+?tPgln_1nyQA}XX_c$7OEDd zm8#uxe^D$K9@^K6G@QGWG~5)gXAzdLS82Ck`v@T@)`MxoFU(n^W-=32ld`y@FeD?>hhh literal 0 HcmV?d00001 diff --git a/exercise-solutions/.DS_Store b/exercise-solutions/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..75ad87205c2caca654bf0aa6aa56bca3c04a9d12 GIT binary patch literal 8196 zcmeHLUu+ab7@u!jV74sYwDPAchdro>$N?=>Ohx2+cL=rBHe5@u<k$yT|R` zl~SsWFD5EJ85QD#i5iV0Jb@UE@lT9V5@P~Yf-zB_cp@fh)X0N=v%6PXN{zlWM0b*% zZ|3_m-^{naZ)ax+ArLRA&4knwLMT3*viVruq;NTZrWL{Ga3P>Q$qw0ttYKxb|M@iS zfDuF>h(Hj5AOb-If(YCS5x_m07dpef?@5C?h(Hj5|78UD_aTB0r!k+7dDQ)MVC5bG zkX9qxE!bwffi&XNm`}$%iV`f~nIiQ}!9QYvXO8@cuNU*_m`6Qx0{-Cxe9z#YP{8$$ z<40U|0x^#Ubr69d0@EYF)25oJL?OeZdGh%^TQGFnX==IzLRIygx$`Kkp*0I?_N4}` zp_HRIB|EMZ_wZh~;tXc}c8_Hl6Ky5CTi1p%Y+=GO9Yr^_qHCi2K@!wPyFV@VL7Rc%T^nkQC!|KYo zuC9WkTc*8RE80jiEM#LW+H;oKUDUX|FJ~F0ylH1xZB8+AB|~wvHp58iM>XVy>|D_@ zjBd-;`7r%yM?2zh*)&mlz_yA`!W}HbSjpCQX*iZ%){;1)oz~GMH)J55zHLc(#j1NY zw6s2$*wK|553$-hNm{hHudLg8*3kMDJExhdZXW6#(p6`003&WX`axYQiX!{5$5is# zcwH?E)i0IhbSbMJF6quNYoyd2N|qZUl(MsXRdoD_rE){n)nC~l%X>ICQPi+j(SQ$g z*J@envH9E-D!QDtQAz4~4BtGnMUT_eEXlowlGO~Rik{iMn~&VMKVH;lb6UTa)!K3&XG^am*f)pfm|U!k)O#gM7o?yE_CXd@(BL3gP=X;i0>khmJPpsnad-}1gje8I zcnw~M)9^lg03X6fa303tEBG3|gDY?qeuLlP54a}G6+!|NYK2B2Dy$RM3vEJN=n#5@ z{X)i@zfj4#eU;_<{6VCXGt0jg6rR6%W`%d`+|e#|+`5@{`8J+Qw=Z0@cu9mO*nJPQ zO@PruZw0WE)4;|4lx%Ua@8;PJpdH7QTNYj}HOz#~UQ8P82<6CU^E~a9(w!8OhRyN1 ztEDv*(}m6Ty3z1@%vH(+w<|S=H;I^rtZICjytICPI2y(*;ZCJiX^V*I$f{k*R%x5W z2!0JjZq<@7?lk2(%p8EddwyARUZ{>x-nRl3Ss%RRPc?id= z { + console.log(com.toUpperCase()); +}); + +itCompany.includes('Nam') ? 1 : 0; +// Filter out companies which have more than one 'o' without the filter method +itCompany.forEach(com => { + const temp = com.split(''); + let count = 0; + for (let i = 0; i < temp.length; i++) { + if (temp[i] == 'o') count++; + } + if (count > 1) console.log(com); +}); +/** + * Sort the array using sort() method +Reverse the array using reverse() method + */ +console.log(itCompany.sort()); +console.log(itCompany.reverse()); + +/** + * Slice out the first 3 companies from the array +Slice out the last 3 companies from the array +Slice out the middle IT company or companies from the array + */ +console.log(itCompany.slice(0, 2)); +console.log(itCompany.slice(itCompany.length - 3, itCompany.length - 1)); +console.log(itCompany.slice(Math.floor(itCompany.length / 2), Math.floor(itCompany.length / 2) + 1)); +/* +Remove the first IT company from the array +Remove the middle IT company or companies from the array +Remove the last IT company from the array +Remove all IT companies +*/ +console.log(itCompany.shift()); +console.log(itCompany.splice(Math.floor(itCompany.length / 2), 1)); +console.log(itCompany.pop()); +console.log(itCompany.splice()); + + +//exercise 2 +let text ='I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.'; +let words = text.split('.').join('').split(' '); +console.log(words); +console.log(words.length); + +const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey']; +/** + * 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' + */ + +if (!shoppingCart.includes('Meat')) shoppingCart.unshift('Meat'); +if (!shoppingCart.includes('Sugar')) shoppingCart.push('Sugar'); +const indexOfHoney = shoppingCart.indexOf('Honey'); +shoppingCart.splice(indexOfHoney, 1); +const indexOfTea = shoppingCart.indexOf('Tea'); +shoppingCart[indexOfTea] = 'Green Tea'; + + +//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. +countries.includes('Ethiopia') ? console.log('Ethiopia') : countries.push('Ethiopia'); + +//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); +// 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: ' + fullStack); +//Excersie 3 +// The following is an array of 10 students ages: js const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24] - Sort the array and find the min and max age - Find the median age(one middle item or two middle items divided by two) - Find the average age(all items divided by number of items) - Find the range of the ages(max minus min) - Compare the value of (min - average) and (max - average), use abs() method +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]; +console.log(ages.sort()); +agesMedianIdx = (ages.length - 1) / 2; +console.log("median age: " + ages[Math.floor(agesMedianIdx)]); +let totalAge = 0; +ages.forEach(age => { + totalAge += age; +}); +const avgAge = totalAge / ages.length; +console.log("Average age: " + avgAge); +const minAge = Math.min(...ages); +console.log("Min age: " + minAge); +const maxAge = Math.max(...ages); +console.log("Max age: " + maxAge); +console.log("Range of the ages: " + (maxAge - minAge)); +console.log("Compare min vs avg: " + Math.abs(minAge - avgAge)); +console.log("Compare max vs avg: " + Math.abs(maxAge - avgAge)); +// 1.Slice the first ten countries from the countries array +console.log("first ten countries: " + countries.slice(0, 10)); +// Find the middle country(ies) in the countries array +const countryMid = countries.length/2; +console.log("middle country: " + countries.slice(countryMid, countryMid + 1)); +// 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. +let firstHalfCountries = []; +let secondHalfCountries = []; +for (let i = 0; i < countries.length; i++) { + const con = countries[i]; + if (i <= countryMid) { firstHalfCountries.push(con);} + else { secondHalfCountries.push(con); } + +} +console.log("first half: " + firstHalfCountries); +console.log("second half: " + secondHalfCountries); \ No newline at end of file diff --git a/exercise-solutions/day1/1.countries.js b/exercise-solutions/day1/1.countries.js new file mode 100644 index 0000000..2a653a1 --- /dev/null +++ b/exercise-solutions/day1/1.countries.js @@ -0,0 +1,13 @@ + let countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', +] diff --git a/exercise-solutions/day1/1.web_techs.js b/exercise-solutions/day1/1.web_techs.js new file mode 100644 index 0000000..52bf291 --- /dev/null +++ b/exercise-solutions/day1/1.web_techs.js @@ -0,0 +1,9 @@ + var webtechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', +]; \ No newline at end of file diff --git a/exercise-solutions/day1/main.html b/exercise-solutions/day1/main.html new file mode 100644 index 0000000..5741a89 --- /dev/null +++ b/exercise-solutions/day1/main.html @@ -0,0 +1,101 @@ + + + + + + + Day 1 : JS refresher + + + +

Hello

+ + + + + + + \ No newline at end of file