From dabc5b49ded44065d365ab1b39f565ffa4d7772e Mon Sep 17 00:00:00 2001 From: Neil Gursahani Date: Tue, 18 Feb 2020 11:35:37 +0100 Subject: [PATCH 1/2] first commit --- starter-code/.DS_Store | Bin 0 -> 6148 bytes starter-code/jasmine/.DS_Store | Bin 0 -> 6148 bytes starter-code/src/functions-and-arrays.js | 2 ++ 3 files changed, 2 insertions(+) create mode 100644 starter-code/.DS_Store create mode 100644 starter-code/jasmine/.DS_Store diff --git a/starter-code/.DS_Store b/starter-code/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3c5d27daf1c894caaa8a9d4668bc09e0e59994cb GIT binary patch literal 6148 zcmeHK&2G~`5S~p^vI#=X0Vy1PLE=zQf(k;akgU)idP5k&0Z?lv7BOFA<5sbkry65RrsythLZwV|rZshSglA z2^8iU31t)?r)n6b8`Yfn8x_#cu1Oy#q=L@q%JWkZM)a~cfZzLR4ADk)G%F@iYO23S zlQb#HUhjvf)f+oEno_o9`&Rp1Fjljm49jUT2=h0(_dG1ek&9m{l`i8jdX>bplg{mD zDl5Yzi*rLxMlnj>zD}}{nhn%68x`g{fgO;tC0i$*?tFf~x92^4yx8;R{r!E!PagFb zijM>^(*_YnK9=P)tL5K~t=G zta{4n0gb7Iim81AmI{6Y&DILz1D-D!nNtT%HooOz-+~+)m!l$xH05C9RPiYl|^J= P{3Bpw;D#0WqYC^2Dg25? literal 0 HcmV?d00001 diff --git a/starter-code/jasmine/.DS_Store b/starter-code/jasmine/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..88ecdce675d115629c323bd83719a23ea5854fb9 GIT binary patch literal 6148 zcmeHKQA-;^5T1=`-K0qKP_U2pBKV*s5nBW)9H~C|RyffID|%;wUO8_icWJ0;%KZ%c zkNOY%Irf{~jVP%;mQZG3_M4lX*}MIA*}Wkm)gAUaL@grHP{vvV&0mE3S!bl?Jqti( z$4DusKCRORy_<;^$2>B?YqvySDW-xh>EZfqVGY}RnK?@`lOT$>$ToEK&I+t&Gm}wq zE-w9@sG*R8#^ab*vnR4dz`{A7|(F^EgIyR$8zD5`j2IhbPJ|6-oW9YGRXpaswwgdpy;kE*se-t?<@ECfm9HIxJ zTq@9|DqmtKmkxj6<3f*>Lzhm<7az*MS@{Y@*|%f-z=o3w9ZD+<2m_B9;Mxz;;`@L1 z>H6OWNh1sh1OF!js=gELbTK!7x6aIx@7e%*3T5HA%He$q40{zLmapQsP%H2UYyd-# Tl|y(S@*`krkVY8zrwsf7nu1`1 literal 0 HcmV?d00001 diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 720d3dcf5..e715934b2 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,5 +1,7 @@ // Iteration #1: Find the maximum +console.log("this is just a test.") + // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; From 886d9eb47e70011233cb3ba31e8e6e0be629fb92 Mon Sep 17 00:00:00 2001 From: Neil Gursahani Date: Tue, 18 Feb 2020 23:23:27 +0100 Subject: [PATCH 2/2] second commit --- starter-code/src/functions-and-arrays.js | 113 +++++++++++++++++- .../tests/functions-and-arrays.spec.js | 3 +- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index e715934b2..ab7278645 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,21 +1,105 @@ // Iteration #1: Find the maximum +function maxOfTwoNumbers(num1, num2) { + if (num1 > num2) { + return num1; + } else if (num1 < num2) { + return num2; + } else { + return num1 && num2; + } +} -console.log("this is just a test.") // Iteration #2: Find longest word const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']; +let emptyArray = []; +let singleWordArray = ['ironhack']; + +function findLongestWord(array) { + if (array.length === 0) { + return null; + } + + + var longestWord = ""; + for (var i = array.length-1; i >= 0; i--){ + if (array[i].length >= longestWord.length) { + longestWord = array[i]; + } + } + return longestWord; + } + + +console.log(findLongestWord(words)); // Iteration #3: Calculate the sum -const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; +const numbers = [5]; + +// var sum = 0; +// numbers.forEach(array => console.log(array[i] + array[i+1]); + +function sumNumbers(array) { + if (array.length === 0) { + return 0; + } + + var sum = 0; + for (i=0; i { + totalL += element.length + avgL = totalL / array.length + }) + if (array.length === 0) { + return null; + } + return avgL +} +console.log(averageWordLength(wordsArr)) + + // Iteration #5: Unique arrays const wordsUnique = [ 'crab', @@ -28,8 +112,31 @@ const wordsUnique = [ 'poison', 'communion', 'simple', - 'bring' + 'bring', ]; +function uniquifyArray(orignalArr) { + let arr = [...orignalArr]; + if (arr.length === 0) { + return null; + } else { + for (let i = 0; i < arr.length; i++) { + for (let j = i + 1; j < arr.length; j++) { + let duplicate = arr.indexOf(arr[i], j); + if (duplicate > -1) { + arr.splice(duplicate, 1); + } + } + } + } + return arr; +} +uniquifyArray(wordsUnique); + +// const uniqueSet = new Set (wordsUnique); + +// const backToArray = [...uniqueSet]; + + // Iteration #6: Find elements const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']; diff --git a/starter-code/tests/functions-and-arrays.spec.js b/starter-code/tests/functions-and-arrays.spec.js index 6de4eaed4..3c0c931bc 100644 --- a/starter-code/tests/functions-and-arrays.spec.js +++ b/starter-code/tests/functions-and-arrays.spec.js @@ -40,11 +40,12 @@ describe('Find the longest word', () => { it('should return null when called with an empty array', () => { expect(findLongestWord([])).toBe(null); }); - + it('should return the word when called with a single-word array', () => { expect(findLongestWord(['foo'])).toBe('foo'); }); + //not sure if my answer for this task is correct it('should return the first occurrence of the word when longest have multiple occurrences ', () => { expect(findLongestWord(['foo', 'bar'])).toBe('foo'); expect(findLongestWord(['bar', 'foo'])).toBe('bar');