Challenges

Functions

Exercise 1

Declare a function named greet that logs "Good Morning" to the console when it is called. Make sure you don't delete the last line, you need to DECLARE your function and then CALL it.

function greet() {
  // your code here!
}

greet();

Exercise 2

Declare a function named greet that takes a person's name as a parameter, and then logs to the console a "Good morning" message for this person. For example, if the input to the function is 'Maggie', the function should log Good morning, Maggie to the console.

function greet() {
  // your code here!
}

greet('Maggie')

Exercise 3

Declare a function named getWeatherAdvice that takes a parameter specifying the weather, and logs to the console advice about you should wear. For example, if the input to the function is 'raining', it should tell you to bring an umbrella. If the input to the function is 'sunny', it should tell you to wear sunglasses.

function getWeatherAdvice(weather) {
  // your code here!
}

getWeatherAdvice('raining');
// should output 'Bring an umbrella!'

// should output 'Wear Sunglasses'
getWeatherAdvice('sunny');
// should output 'Wear Sunglasses'

Exercise 4

Declare a function named divide that divides two numbers and returns the result. For example, if the inputs to the function are 10 and 2, it should return 5.

function divide(numberOne, numberTwo) {
  // your code here!
}

divide(10, 2);
// should return 5

Exercise 5

Declare a function named largestNum that takes two numbers and returns the largest one. For example, if the inputs are 3 and 8, it should return 8.

function largestNum(num1, num2) {
  // your code here!
}

largestNum(3, 8)
// should return 8

Exercise 6

Declare a function named areStringsEqual that checks if two strings are the same, and returns true or false. For example, if the inputs are 'sunshine' and 'sunshine', the function should return true. If the inputs are 'sunshine' and 'pizza', it should return false.

function areStringsEqual(str1, str2) {
  // your code here!
}

areStringsEqual('sunshine', 'sunshine');
// should return true

areStringsEqual('sunshine', 'pizza');
// should return false

Intermediate Challenges (Stretch)

FizzBuzz

Fizz Buzz is a famous programming problem. To solve it, write some code that prints all numbers from 1 to 100. But, instead of the numbers, for multiples of 3 print 'Fizz', for multiples of 5 print 'Buzz', and for multiples of both 3 and 5 print 'FizzBuzz'.

For example, if I was solving FizzBuzz for the numbers 1-15, this would be my expected output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

To tackle this problem, grab a classmate and "pair program". Pair programming is a common practice. It's when two developers sit down to work on a problem together. One will usually "drive" (that is write the code), while the other helps solve the problem. It's common to take turns writing the code.

You can write the code on one computer or on both, but do work through the logic of the problem together.

Tip: When you're faced with a problem like this one, there are several good strategies to take:

  1. Work out how you, as a human being, would solve the problem (that is, without any code).
  2. Break the problem down into smaller pieces (for instance, start by just printing the numbers 1 to 100 to the screen, then build from there).
  3. Work slowly and incrementally.

Tips & Tricks

Remainder

In order to determine whether a number is a multiple of 3 or 5, you may need to use the remainder operator%. The remainder operator (technically known as the modulo operator) returns the remainder (how much is left over) when one number is divided by the other.

10 % 7
// would evaluate to 3

10 % 2
// would evaluate to 0

Yellow Pager

Another fun programming challenge is the Yellow Pager.

Write a function that accepts a 10-character string of letters and returns a corresponding phone number string. If the input letter string is not exactly 10 characters, the function should returnfalseinstead.

Use the following number to letter mapping to turn a string into a phone number:

2 -> A B C
3 -> D E F
4 -> G H I
5 -> J K L
6 -> M N O
7 -> P Q R S
8 -> T U V
9 -> W X Y Z

When solving a problem like this, it's a good idea to start with some examples, so you know what the correct output should be:

The stringLighthouseshould output5444846873.

The string Helloworld should output4355696753.

Keep in mind that there are several ways to solve this problem, so if a classmate takes a different approach than you, that's okay.

Pair with another classmate and work through the logic together! Remember it's always best to come up with a plan of how you're going to solve this problem _before _you start writing code.

You can map out the steps you're going to take on a piece of paper, if you find that helpful.

JavaScript reference

.split()

This string function can be used to split a string into an array of characters.

"hello".split('')
// will return ['h', 'e', 'l', 'l', 'o']

.toUpperCase()

This string function converts all of the letters in the string to uppercase.

"hello".toUpperCase()
// will return "HELLO"

.toLowerCase()

This string function converts all of the letters in the string to lowercase.

"HELLO".toLowerCase()
// will return "hello"

For loop

Loops over all of the elements in an array

var cities = [
 'Toronto',
 'Ottawa',
 'London'
]

for(var i = 0; i < cities.length; i++) {
 console.log(cities[i]);
} 

// will log to the console:
// Toronto
// Ottawa
// London

results matching ""

    No results matching ""