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
multiplyNumbers
.What do we call firstNumber
and secondNumber
?
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?
What is foo
equal to?
42
36
15
undefined
foo
is equal to 36
.What is bar
equal to?
55
19
60
undefined
bar
is equal to 60
.What is baz
equal to?
6
5
42
undefined
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
calculateArea
.What are the names of the parameters this function accepts?
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?
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
area
is equal to 60
.What Data Type is the units
parameter?
units
is a String.What is boxArea
equal to?
60
17 sq ft
"60 sq ft"
undefined
boxArea
is equal to "60 sq ft"
.What Data Type is the boxArea
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?
myText
and processedText
.myText
and processedText
.myText
and processedText
.myText
and processedText
.myText
and processedText
without initializing them to any value.What Data Type is stopWords
?
stopWords
is initialized as an Object.When newWordArray
is declared, how is it initialized (what value is it made equal to)?
null
0
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?
let
are global.let
are accessible to the scope in which they are declared and any children scopes.scope
apply to conditionals.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
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?
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"]
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.