Variables & Identifiers - Exercise 2
Question 1: Read an Outer Binding Completed
Define a value in the outer function. Return it from an inner function and return that result.
Expected output: "shared" (print this with console.log)
function readOuterBinding(label) {
// Write your solution here
}
console.log(readOuterBinding("shared"));function readOuterBinding(label) {
const message = label;
function read() { return message; }
return read();
}
console.log(readOuterBinding("shared"));Question 2: Accumulate with var Completed
Use a var variable to accumulate the two amounts and return the result.
Expected output: 14 (print this with console.log)
function accumulateWithVar(first, second) {
// Write your solution here
}
console.log(accumulateWithVar(8, 6));function accumulateWithVar(first, second) {
var total = first;
total += second;
return total;
}
console.log(accumulateWithVar(8, 6));Question 3: Return a Shadowed Value Completed
Declare an outer label, shadow it inside a block with the supplied inner label, and return the inner label.
Expected output: "inside" (print this with console.log)
function returnShadowedLabel(outer, inner) {
// Write your solution here
}
console.log(returnShadowedLabel("outside", "inside"));function returnShadowedLabel(outer, inner) {
const label = outer;
{ const label = inner; return label; }
}
console.log(returnShadowedLabel("outside", "inside"));Question 4: Update a let Counter Completed
Create a let counter from the supplied start value, increment it once, and return it.
Expected output: 10 (print this with console.log)
function incrementLetCounter(start) {
// Write your solution here
}
console.log(incrementLetCounter(9));function incrementLetCounter(start) {
let counter = start;
counter++;
return counter;
}
console.log(incrementLetCounter(9));Question 5: Prefix a Reserved Word Completed
Create a safe identifier string by prefixing the supplied lowercase word with "user_".
Expected output: "user_class" (print this with console.log)
function prefixReservedWord(word) {
// Write your solution here
}
console.log(prefixReservedWord("class"));function prefixReservedWord(word) {
return "user_" + word;
}
console.log(prefixReservedWord("class"));Question 6: Capture Block Variables Completed
Return an array showing the supplied outer and inner values using a var and a separately block-scoped let.
Expected output: ["visible","inside"] (print this with console.log)
function captureBlockBindings(outer, inner) {
// Write your solution here
}
console.log(captureBlockBindings("visible", "inside"));function captureBlockBindings(outer, inner) {
var outside = outer;
let captured;
{ let inside = inner; captured = inside; }
return [outside, captured];
}
console.log(captureBlockBindings("visible", "inside"));Question 7: Use a Constant Tax Rate Completed
Store 0.1 in a const variable and return the tax for the supplied price.
Expected output: 8 (print this with console.log)
function fixedRateTax(price) {
// Write your solution here
}
console.log(fixedRateTax(80));function fixedRateTax(price) {
const rate = 0.1;
return price * rate;
}
console.log(fixedRateTax(80));Question 8: Update a Constant Object Completed
Create a const object with a count property, increase that property by one, and return the count.
Expected output: 7 (print this with console.log)
function incrementConstantRecord(initial) {
// Write your solution here
}
console.log(incrementConstantRecord(6));function incrementConstantRecord(initial) {
const record = { count: initial };
record.count += 1;
return record.count;
}
console.log(incrementConstantRecord(6));Question 9: Catch a Strict Assignment Error Completed
In strict mode, try assigning to an undeclared variable. Return an array containing the resulting error name and supplied label.
Expected output: ["ReferenceError","test"] (print this with console.log)
function strictAssignmentError(label) {
// Write your solution here
}
console.log(strictAssignmentError("test"));function strictAssignmentError(label) {
'use strict';
try { undeclaredExerciseValue = 1; } catch (error) { return [error.name, label]; }
}
console.log(strictAssignmentError("test"));Question 10: Join Greeting Bindings Completed
Store the greeting and name in variables, then return them joined with a comma and space.
Expected output: "Welcome, Ria" (print this with console.log)
function joinGreetingBindings(greeting, name) {
// Write your solution here
}
console.log(joinGreetingBindings("Welcome", "Ria"));function joinGreetingBindings(greeting, name) {
const opening = greeting;
const recipient = name;
return opening + ", " + recipient;
}
console.log(joinGreetingBindings("Welcome", "Ria"));