JS Control Flow Challenges
Basic Challenges
- Which of the following are truthy values? (hint: try
if("abc"){console.log("I'm truthy!")}
in the JS console)- 1
- "abc"
- ""
- []
- {}
- -1
- (1+1)
- 0
- 3.14159
- Object
- Log to the console "This is awesome!" 25 times.
- Create a snippet inside of developer tools. Create snippets to do the rest of the challenges.
- In your snippet, create a new variable that is an array of 4 phrases:
Howdy there
,OMG
,javascript
, andPair Programming
. - Loop over the array and console log each phrase.
- Loop over the array and log each phrase to the console if its total length is 4 or longer. Otherwise, console log that the phrase is too short.
- Come up with three different ways to break a
for
loop that result in three different errors. What three different errors did you get? - Jimmy loves roller coasters, but there are a bunch of rules (ugh!) for riding:
For starters, it costs 5 tokens. Here's how we might code that:
var tokens = 3; // Jimmy's tokens
// Can he ride?
if ( tokens >= 5 ) {
console.log("Step right up!");
} else {
console.log("Sorry, you can't ride")
}
Edit the code above to check the following additional Requirements:
Must be at least 4ft tall
Must be at least 12 years old
Replace the prevoius rule: now riders under 12 must be accompanied by an adult
(If the boss isn't looking, you can sneak in!)
Riders with a park pass get in free.
Stretch Challenges
- In a snippet, create a new variable that is an array containing 5 objects, each of which has the keys
name
andage
. You can make up the names and ages for your objects. - Log to the console the name value of each object.
- Create and log an array with the age of each object in months (assume the original ages were in years).
- Find and log the sum of the ages.
- Log to the console only the name of the oldest person.
- Log to the console the index of each element in the array.
- Create and log an array containing only the objects with an age over 20.
- Create and log an array of all the names, in which any names that begin with a consonant are upper case.
- Create and log an array that is the original array in a random order.