person | challenge |
---|---|
partner 1 | reverse words |
partner 2 | title case words |
partner 1 | fizz-buzz |
partner 2 | anagrams |
partner 1 | unique-string |
partner 2 | unique-char |
pair stretch 1 | equal-sides |
pair stretch 2 | happy-numbers |
For today's work, create a github repo and work in VSCode
Let's take a couple of minutes to get your document ready!
String code challenges usually are array challenges and involve changing the string into an array of letters or words (or both).
Go ahead and look these methods up on MDN if you need them today!
- array of letters
const letters = word.split('');
- sentence into an array of words
const words = sentence.split(' ');
Strings are "array like" in that you can access by index:
const firstLetter = word[0];
However, you cannot assign a letter:
word[0] = word[0].toUpperCase();
There are code challenges problems specifically design to trip you up on this.
Instead, use .slice(startIndex, endIndex)
to make new string from pieces.
Join the elements of an array to make a string, use join:
const word = letters.join('');
const sentence = words.join(' ');
While you can use loops to manage things, often using .map
after .split
is just the thing you need
toUpperCase
toLowerCase
padLeft
padRight
The other thing that crops up with string challenges is using regex. This is its own topic and we won't see regex today
Leverage chaining to write clean easy to "see" code:
function doString(str) {
return str
.split('')
.map(letter => {
})
.join('');
}
reverse sentence words
and title case words
Number problems typically involve word-problem type math, or splitting numbers into digits.
- The use of modulo (remainder) is prevalent ("evenly divisible"):
const isEven = x % 2 === 0
- Call
toString
before attempting tosplit
a number into digits:const digits = number.toString().split('');
- Conversion problems are common (how many seconds in year)
Handy way to return one of two options based on a condition:
return x < -1 ? 'negative' : 'positive';
oddish-evenish
and at
Most string
and number
problems are usually control flow problems, applying conditional logic in the right order and managing looping and lists of things.
They also require declaring variables in the correct scope in relation to any loops and conditional statements. Scope is entirely dependent on code blocks.
Some problem say "go up to n
". Typically best to use a classic for
loop and set the limit at n
. Then you can count to n
from 0
(or 1
or whatever)
There are classes of problems that benefit from sort
ing and/or having a unique set of things (de-duplication).
The default sort()
in JavaScript provides lexical (dictionary) sorting by default.
fizz-buzz
- classic code challenge problems with an "order matters" twist
anagram
- making hard problems easy by using the built-in methods
Sorting can make hard problems easier. By having ordered content, you can loop through an array and look for matching values between adjacent elements.
By passing an array into a Set
constructor, and then spreading back into an array, you can get a unique list of things (and then maybe back to a string if needed):
const set = new Set(numbers);
const unique = [...set];
// or
const unique = [...new Set(numbers)];
unique-string
- classic code challenge problems with an "order matters" twist
unique-char
- making hard problems easy by using the built-in methods
These problems combine control flow, number and string manipulation. Take your time on problem definition and design to break down the steps
equalSides
happyNumbers
(see next section)
Sometimes a problem will require successive iterations, but you don't really know when it is going to end until a certain condition is met. You can use the following construct, leveraging break
or return
:
// notice this is declared and initialize
// before the loop
let x = 0;
let y = 100;
while(true) {
if(y === x) return true;
if(y < 0) return false;
x = x + 2;
y = y - 1;
}