From a26573b60b0ac03ff4f2536b0e956c1c4ab58547 Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 8 Aug 2017 12:23:20 -0400 Subject: [PATCH 1/8] completed longestString --- longestString/longestString.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/longestString/longestString.js b/longestString/longestString.js index 35b887c..e37cdc3 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -2,3 +2,20 @@ * Write a function that accepts an array of strings. * Return the longest string in the array. */ + +// 1. identify test cases +// ['abc', 'a', 'b'] -> 'abc' +// ['abc', 'def'] -> 'def' +// [] -> null + +/* +function longestString strings + sort string by length + return string index 0 +*/ + + +const longestString = (strings) => { + strings.sort((a,b) => b.length - a.length) + return strings[0]; +} \ No newline at end of file From 3a1281846a9756e228424102eb4ab94264c7d1df Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 8 Aug 2017 12:28:09 -0400 Subject: [PATCH 2/8] Completed longestString --- longestString/longestString.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longestString/longestString.js b/longestString/longestString.js index e37cdc3..c0c8e1b 100644 --- a/longestString/longestString.js +++ b/longestString/longestString.js @@ -18,4 +18,4 @@ function longestString strings const longestString = (strings) => { strings.sort((a,b) => b.length - a.length) return strings[0]; -} \ No newline at end of file +}; \ No newline at end of file From 6ad4cade4fb35e726f516f09b4d0bc93f5f34ec8 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 9 Aug 2017 12:51:35 -0400 Subject: [PATCH 3/8] Completed reverseCase --- reverseCase/reverseCase.js | 65 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 reverseCase/reverseCase.js diff --git a/reverseCase/reverseCase.js b/reverseCase/reverseCase.js new file mode 100644 index 0000000..9a3c999 --- /dev/null +++ b/reverseCase/reverseCase.js @@ -0,0 +1,65 @@ +/* + * 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. + */ + +// 1. identify test cases +// 'Hello World' -> 'hELLO wORLD' +// 'tE sT' -> 'TeSt' +// '' -> '' + +/* +function reverseCase + new string intitalizes + loop through string + if string.charCodeAt(i) is between 64 and 91 + change letter to lower case add to new string + if letter is between 96 and 123 + change to uppercase add to new string + else + add to new string + */ + +const reverseCase = (string) => { + let newString = ''; + for (let i = 0; i < string.length; i++) { + if (string.charCodeAt(i) > 64 && string.charCodeAt(i) < 91) { + newString += string[i].toLowerCase(); + } + else if (string.charCodeAt(i) > 96 && string.charCodeAt(i) < 123) { + newString += string[i].toUpperCase(); + } + else { + newString += string[i]; + } + } + return newString; +} + +//An alternative way using forEach && push instead of iterating through string +const reverseCaseAlt = (string) => { + const newString = []; + string.split('').forEach((letter) => { + if (letter.toLowerCase() === letter) { + newString.push(letter.toUpperCase()); + } + else if (letter.toUpperCase() === letter) { + newString.push(letter.toLowerCase()); + } + else { + newString.push(letter) + } + }) + return newString.join(''); +} + + +console.log(reverseCase('Hello World')); +console.log(reverseCase('tE sT')); +console.log(reverseCase('')); + + +console.log(reverseCaseAlt('Hello World')); +console.log(reverseCaseAlt('tE sT')); +console.log(reverseCaseAlt('')); \ No newline at end of file From 7c969d80b140d667c7d112fecb73785421730251 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 17 Aug 2017 13:24:17 -0400 Subject: [PATCH 4/8] minor changes --- forLoopTimeout/forLoopTimeout.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/forLoopTimeout/forLoopTimeout.js b/forLoopTimeout/forLoopTimeout.js index 574a437..90bf968 100644 --- a/forLoopTimeout/forLoopTimeout.js +++ b/forLoopTimeout/forLoopTimeout.js @@ -8,9 +8,9 @@ /* By the time console.log is being called the final value of i is 11 because it has already ran through the loop this is due to closure -when creating a function it gets hoisted to the global scope. i is not within its scope until after the loop runs +when creating settimeout it gets hoisted to the global scope. i is not within its scope until after the loop runs this is why creating a function within a loop is bad practice -in es6 an arrow function would solve this issue because it has lexical scope +in es6 let would not have a block scope */ for (var i = 1; i <= 10; i++) { wrapper(i); @@ -24,5 +24,4 @@ function wrapper(num) { // By setting a function around setTimeout called wrapper we can take i each time 1 is added and // call wrapper with i as the argument. This i value is being saved at that point in time and -// console logged by setTimeout at each instance -// essentially calling a function and creating it have different closures. \ No newline at end of file +// console logged by setTimeout at each instance \ No newline at end of file From 26aa7b09b06e33ced56d467a16ca0a6c52cae276 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 18 Aug 2017 13:28:44 -0400 Subject: [PATCH 5/8] minor changes --- largestPrimePalindrome/largestPrimePalindrome.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/largestPrimePalindrome/largestPrimePalindrome.js b/largestPrimePalindrome/largestPrimePalindrome.js index 238f448..351b7b6 100644 --- a/largestPrimePalindrome/largestPrimePalindrome.js +++ b/largestPrimePalindrome/largestPrimePalindrome.js @@ -14,7 +14,7 @@ // create function that takes in num // iterate from top down to find the highest number that is a palindrome // this way for higher numbers you dont have to iterate as far - // set a counter and j at 2 + // set a counter at 0 and j at 2 // use a while loop to test whether prime // the while loop will help to cut down on time because as soon as you find out its not // if the the i is found to be divisible by j the loop is stopped @@ -26,6 +26,8 @@ for (let i = num; i > 0; i--) { const str = i.toString(); if (str === str.split('').reverse().join('')) { + let j = 2; + let count = 0; while (count === 0 && j <= (i/2)) { if (i % j === 0) count++; j++; From a0dc216590f50af905a9bc611edbb9ee54e5366a Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 24 Aug 2017 12:58:33 -0400 Subject: [PATCH 6/8] minor change --- stringCompression/stringCompression.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index baf33f7..fa4701c 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -39,5 +39,5 @@ const stringCompression = (str) => { } } if (newstr.length < str.length) return newstr; - return str; + return newstr; } From bc2fdf5b83b639f1e2716a01e87bb2820d160bd0 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 24 Aug 2017 12:59:39 -0400 Subject: [PATCH 7/8] nvm --- stringCompression/stringCompression.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index fa4701c..baf33f7 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -39,5 +39,5 @@ const stringCompression = (str) => { } } if (newstr.length < str.length) return newstr; - return newstr; + return str; } From 7c140e2819499e3d8703df862b24f850eeea583f Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 24 Aug 2017 13:18:17 -0400 Subject: [PATCH 8/8] shorter code --- stringCompression/stringCompression.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/stringCompression/stringCompression.js b/stringCompression/stringCompression.js index baf33f7..d4ea7eb 100644 --- a/stringCompression/stringCompression.js +++ b/stringCompression/stringCompression.js @@ -15,7 +15,7 @@ create function that takes in string if the previous character is the same as the current character add one to the characters key value in memo if the next character is different from the current character - add the current character add the amount of times in a row we've seen that character to the new string + add the current character and the amount of times in a row we've seen that character to the new string if the new string's length is less than the original string return the new string return the original string */ @@ -28,16 +28,9 @@ const stringCompression = (str) => { let newstr = ''; const memo = {}; for (let i = 0; i < str.length; i++) { - if (i === 0 || str[i - 1] !== str[i]) { - memo[str[i]] = 1; - } - if (str[i - 1] === str[i]) { - memo[str[i]] += 1 - } - if (str[i + 1] !== str[i]) { - newstr += str[i] + memo[str[i]]; - } + if (i === 0 || str[i - 1] !== str[i]) memo[str[i]] = 1; + if (str[i - 1] === str[i]) memo[str[i]]++; + if (str[i + 1] !== str[i]) newstr += str[i] + memo[str[i]]; } - if (newstr.length < str.length) return newstr; - return str; + return newstr.length < str.length ? newstr : str; }