Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions brainTeasers/waterJugs.md
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion isUnique/isUnique.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
*/

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
14 changes: 14 additions & 0 deletions longestString/longestString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
8 changes: 7 additions & 1 deletion removeDuplicates/removeDuplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@

const removeDuplicates = (arr) => {
//code here...
};
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);
23 changes: 22 additions & 1 deletion reverseCase/reverseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
*/

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