Exercises
Recap Questions
WIP
Exercises
Guess the secret word
Write a function called guessSecretWord that takes a word as a parameter. The function should log "You guessed right!" if the word is 's3cr3t', and "Not even close." otherwise.
Write two or more function calls with appropriate arguments that test if the code works fine.
Possible solution
function guessSecretWord(guess) { if (guess == 's3cr3t') { console.log('You guessed right!'); } else { console.log('Not even close.'); } } guessSecretWord('Abracadabra'); guessSecretWord('Hocus Pocus'); guessSecretWord('s3cr3t');
Toggle Console Output
- 'Not even close.'
- 'Not even close.'
- 'You guessed right!'
Deciding what to do tonight
Write a function called showPlans with three parameters - two strings that represent the plans for the evening, and the time of the day as a number.
- if the passed time is later than 20, log "It's too late, I will { the second plan }";
- else, log "I have plenty of time, I will { the first plan }".
Write two or more function calls with appropriate arguments that test if the code works fine.
Possible solution
function showPlans(planA, planB, time) { if (time > 20) { console.log("It's too late, I will", planB); } else { console.log('I have plenty of time, I will', planA); } } showPlans('go to the gym', 'watch a movie', 22); showPlans('go to the gym', 'watch a movie', 18);
Toggle Console Output
- It's too lateI'll watch a movie
- I have plenty of timeI'll go to the gym
Bonus: make the function take a fourth parameter that defines when the late time starts, and use it inside the function accordingly.
Possible solution
function showPlans(planA, planB, time, lateAfter) { if (time > lateAfter) { console.log("It's too late, I will", planB); } else { console.log('I have plenty of time, I will', planA); } } showPlans('go to the gym', 'watch a movie', 22, 21); showPlans('go to the gym', 'watch a movie', 18, 21);
Toggle Console Output
- It's too lateI'll watch a movie
- I have plenty of timeI'll go to the gym