Created
September 25, 2018 18:18
-
-
Save mattbrannon/22d3bf2eb8fcbd080fad4f4dc6281cc9 to your computer and use it in GitHub Desktop.
Problem solving process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write a function that takes a string as input and returns the string reversed. | |
Do NOT use the native .reverse() method. | |
reverseString("never") // returns "reven" | |
Strategy: | |
Given a string, split the string in an array of characters | |
Loop backwards through the array starting from the last index | |
Make a new string by concatenating each character | |
return the new string | |
Steps: | |
Input: "never" | |
Desired output: "reven" | |
Iteration | Array of characters | current Index | current Character | new String | |
1 | never | 4 | r | r | |
2 | neve | 3 | e | re | |
3 | nev | 2 | v | rev | |
4 | ne | 1 | e | reve | |
5 | n | 0 | n | reven | |
*/ | |
// function reverseString(string){ | |
// split string into an array of characters | |
// declare a new string to hold the reversed characters of our original string | |
// get the length of the array and assign it to a variable "len" | |
// starting at the last index | |
// assign the character of each index to the new string | |
// subtract 1 from len variable | |
// return the new string | |
// } | |
/* findFirstNonRepeatChar | |
Given a string s, find and return the first instance of a non-repeating character in it. | |
If there is no such character, return null. | |
findFirstNonRepeatChar("abacabad") // returns "c" | |
*/ | |
// create an empty object to hold each letter as a key in our string | |
// loop through each letter in the string | |
// if the letter is not a key in the object | |
// then add the letter as a key to the object with a value of 1 | |
// else if the letter is already a key in the object then return the letter | |
/* | |
sumNestedArray | |
Given an array with nested arrays at each index containing a series of numbers. | |
Return a flattened array with the sum of those numbers at the matching index. | |
sumNestedArray([[12,12],[6,6]]) // returns [24, 12] | |
*/ | |
// create a new empty resultArray to hold the sum of our numbers | |
// for each array in our outer array | |
// calculate the sum of the numbers | |
// push the sum of the numbers to our resultArray | |
// return the resultArray | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good job, but make sure to use those transformation steps for all problems.