From df0070386581a42ee4c8cd83b10be77e0f7ebf88 Mon Sep 17 00:00:00 2001 From: Kelli Date: Wed, 9 Aug 2017 15:03:55 -0400 Subject: [PATCH 1/5] Solve coding challenge --- longestString/longestString.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..dd3cf51 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,17 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +const longestString = (arr) => { + let longest = 'abcd'; + for (let i = 0; i < arr.length; i++){ + if (arr[i].length > longest.length){ + longest = arr[i]; + } + } + return longest; + +}; + +const strings = ['a', 'abcd', 'abc']; +console.log(longestString(strings)); From 2f980bc6115c8df94c991d9d7093a931e1502142 Mon Sep 17 00:00:00 2001 From: Kelli Date: Thu, 10 Aug 2017 13:38:49 -0400 Subject: [PATCH 2/5] Solve reverse case --- reverseCase/reverseCase.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js index f1051f5..06f239b 100644 --- a/reverseCase/reverseCase.js +++ b/reverseCase/reverseCase.js @@ -2,4 +2,25 @@ * Write a function that reverses the case of each letter in the strings that it receives. * Example: 'Hello World' -> 'hELLO wORLD' * Assume that each string will contain only spaces and letters. - */ \ No newline at end of file + */ + +const reverseCase = (str) => { + /** Create an empty string to build upon + * then iterate over each letter on string (for loop) + * compare if str[i] to str[i].toLowerCase + * emptyString += str[i].toUpperCase() + * else emptyString += str[i].toLowerCase() + * return emptyString*/ + let newStr = ''; + for (let i = 0; i < str.length; i++){ + if (str[i] === str[i].toLowerCase()){ + newStr += str[i].toUpperCase(); + } + else{ + newStr += str[i].toLowerCase(); + } + } + return newStr; +} + +console.log(reverseCase('HdaYhgksFewCbfjEfjVjeBNU')); \ No newline at end of file From 4bdec9567104552b9eb1eb164f830e1e8ccdbe2b Mon Sep 17 00:00:00 2001 From: Kelli Date: Mon, 14 Aug 2017 12:31:39 -0400 Subject: [PATCH 3/5] solve removeDuplicates exercise --- removeDuplicates/removeDuplicates.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/removeDuplicates/removeDuplicates.js b/removeDuplicates/removeDuplicates.js index 970f719..ab3bf3a 100644 --- a/removeDuplicates/removeDuplicates.js +++ b/removeDuplicates/removeDuplicates.js @@ -10,4 +10,10 @@ const removeDuplicates = (arr) => { //code here... -}; \ No newline at end of file + var array = arr.filter((v, i, a) => a.indexOf(v) === i); + return array; +}; +var arr = [1, 1, 1, 2, 2, 3, 4, 5, 5]; +var newArr = removeDuplicates(arr); + +console.log(newArr); \ No newline at end of file From 8f197ebc40ea844f0a593f1ea6563df9d03f4b24 Mon Sep 17 00:00:00 2001 From: Kelli Date: Tue, 15 Aug 2017 12:29:53 -0400 Subject: [PATCH 4/5] solve waterJugs exercise --- brainTeasers/waterJugs.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/brainTeasers/waterJugs.md b/brainTeasers/waterJugs.md index 5276fc0..0031b48 100644 --- a/brainTeasers/waterJugs.md +++ b/brainTeasers/waterJugs.md @@ -1 +1,6 @@ You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible. + +fill 5 quart jar +move 5 quart jar water to 3 quart, now 5 quart jar has 2 quarts water in it. +remove all the water from 3 quart jar, and move the 2 quart water from the 5 quart jar to 3 quart jar, now the 3 quart jar has 2 quarts water in it. +fill the 5 quart jar, and use the 5 quart jar to fill the 3 quart jar which already have 2 quarts water in it. Then what was left in the 5 quart jar will be 4 quarts of water From b286a2313db7f937200eb87558cf9e6ad9a007ec Mon Sep 17 00:00:00 2001 From: Kelli Date: Tue, 15 Aug 2017 12:41:36 -0400 Subject: [PATCH 5/5] solve isUnique exercise --- isUnique/isUnique.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/isUnique/isUnique.js b/isUnique/isUnique.js index 6c9caf5..68dc661 100644 --- a/isUnique/isUnique.js +++ b/isUnique/isUnique.js @@ -1,3 +1,15 @@ /* Implement an algorithm to determine if a string has all unique characters. * What if you cannot use additional data structures? - */ \ No newline at end of file + */ + +const isUnique = (str) => { + const strSet = new Set(); + for (let i = 0; i < str.length; i++) { + if (strSet.has(str[i])) return false; + strSet.add(str[i]); + } + return true; + }; + + console.log(isUnique('abcdhijklmnopqrstuv')); // true + console.log(isUnique('abcdefga')); // false