diff --git a/starter-code/.DS_Store b/starter-code/.DS_Store new file mode 100644 index 000000000..3c5d27daf Binary files /dev/null and b/starter-code/.DS_Store differ diff --git a/starter-code/jasmine/.DS_Store b/starter-code/jasmine/.DS_Store new file mode 100644 index 000000000..88ecdce67 Binary files /dev/null and b/starter-code/jasmine/.DS_Store differ diff --git a/starter-code/src/functions-and-arrays.js b/starter-code/src/functions-and-arrays.js index 720d3dcf5..ab7278645 100644 --- a/starter-code/src/functions-and-arrays.js +++ b/starter-code/src/functions-and-arrays.js @@ -1,19 +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; + } +} + // 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', @@ -26,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');