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 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 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)); 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 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