Extra Practice

Remember: Learning to program takes practice! It helps to see concepts over and over, and it's always good to try things more than once. We learn much more the second time we do something. Use the exercises and additional self-checks below to practice.

1. Multiplying Two Numbers

Review the code, then answer the questions below.

function multiplyNumbers(firstNumber=2, secondNumber=4){
    let product = firstNumber * secondNumber;
    return product;
}

let foo = multiplyNumbers(3,12);

let bar = multiplyNumbers(15,4);

let baz = multiplyNumbers("two","three");

What is the name of the function?

function multiplyNumbers foo bar The name of the function is multiplyNumbers.

What do we call firstNumber and secondNumber?

variables parameters attributes indices In this function declaration, firstNumber and secondNumber are the parameters that this function accepts.

The firstNumber and secondNumber parameters have values assigned in the function declaration. What do we call this specific type of parameter?

assigned parameters declared parameters default parameters arguments Parameters that have a default value specified in the function declaration are called "default parameters".

What is foo equal to?

42 36 15 undefined The variable foo is equal to 36.

What is bar equal to?

55 19 60 undefined The variable bar is equal to 60.

What is baz equal to?

6 5 42 undefined The variable baz is equal to undefined because we cannot multiply two Strings.

2. Calculating Area of a Box

function calculateArea(length, width, units="sq ft"){
    let area = length * width;
    return `${area} ${units}`;
}

let boxLength = 5;
let boxHeight = 12;

let boxArea = calculateArea(boxLength, boxHeight);

What is the name of the function?

function boxLength boxArea calculateArea The name of the function is calculateArea.

What are the names of the parameters this function accepts?

boxLength length width units boxHeight In this function declaration, length, width, and units are the parameters we can use with this function.

What is the expected Data Type for the length and width parameters?

Number Array String Boolean The length and width parameters are expected to be Numbers so they can be multiplied together to calculate the area.

What is area equal to?

42 17 60 undefined The variable area is equal to 60.

What Data Type is the units parameter?

Number Array String Boolean The parameter units is a String.

What is boxArea equal to?

60 17 sq ft "60 sq ft" undefined The variable boxArea is equal to "60 sq ft".

What Data Type is the boxArea variable?

Number Array String Boolean The variable boxArea is a String.

3. Scoping Variables


let myText,
    processedText;

const stopWords = { // stopWords is an Object defining words that should not be capitalized.
    'of': true,
    'the': true,
    'a': true,
    'an': true,
    'and': true,
    'to': true
}


function capitalizeText(text){
    // This function expects a String of text. It breaks apart the String into an 
    // Array of words, then it capitalizes the first letter of each word. Once it has
    // capitalized all words, it returns a new String with capitalized text.

    let wordArray = text.split(' '); // Split on space characters.
    let newWordArray = []; // Initialize an Array to store results.

    for (word of wordArray){
        if (!stopWords[word]){ // Check to make sure word is not in stopwords list.
            let newFirstLetter = word[0].toUpperCase(); // Change first letter in `word` to uppercase.
            let slicedWord = word.slice(1); // Get the rest of the word after the first letter.
            let newWord = newFirstLetter + slicedWord; // Put the word back together.
            newWordArray.push(newWord); // Add the newWord to the newWordArray of capitalized words.
        } else {
            newWordArray.push(word); // If we hit a stopWord, just put that word back in the list without altering.
        }
    }

    var newText = newWordArray.join(' ');
    return newText; // Return the string.
}

myText = "association of code writers";
processedText = capitalizeText(myText);
console.log(processedText);

The first line of the code above does what?

Initializes myText and processedText. Defines myText and processedText. Declares myText and processedText. Establishes myText and processedText. The first line of the code above declares myText and processedText without initializing them to any value.

What Data Type is stopWords?

String Number Object Array Boolean The constant variable stopWords is initialized as an Object.

When newWordArray is declared, how is it initialized (what value is it made equal to)?

null empty Array 0 empty Object The variable newWordArray is declared and initialized in the same line. It is initialized to an empty Array.

Why can we use stopWords inside the conditional within the capitalizeText function?

Because variables declared with let are global. Because variables declared with let are accessible to the scope in which they are declared and any children scopes. Because code is magic. Because special rules of scope apply to conditionals. We can access stopWords within the function because variables declared with let are accessible to the scope in which they are declared and any children scopes. The function, and the loop and conditional within, are all structures that create "child scopes" (and these scopes would all contain stopWords).

What variables defined within the scope of the condition inside capitalizeText() are ONLY accessible WITHIN the "success" or "true" clause of the conditional? (Only the top part of the statement between the first set of braces.)

word newFirstLetter slicedWord newWord newWordArray stopWords The variables newFirstLetter, slicedWord, and newWord are only accessible within the "true" clause of the conditional. word is accessible in the for ... of loop (and, therefore, in all clauses of the conditional). newWordArray is available throughout the entire function, and stopWords is available throughout the entire script.

Consider this block of code:

for (word of wordArray){ if (!stopWords[word]){

In this case, what does word equal?

The value of each index in the Array as the loop moves through them. The value of the entire Array. The index (number) of each item in the Array. "42"? The word variable contains the value of each index in the Array as the loop moves through them.

Consider this block of code:

for (word of wordArray){ if (!stopWords[word]){

If we are looping through the wordArray and the value of word is "of", what is the value of stopWords[word]?

stopWords.of stopWords.word stopWords["of"] stopWords.["of"] The variable stopWords[word] is evaluates equal to stopWords.of or stopWords["of"].

Visit Content Online

The content on this page has been removed from your PDF or ebook format. You may view the content by visiting this book online.

results matching ""

    No results matching ""