Variables & Identifiers - Exercise 2

Question 1: Read an Outer Binding

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)

javascript

function readOuterBinding(label) {
  // Write your solution here
}

console.log(readOuterBinding("shared"));

Question 2: Accumulate with var

Use a var variable to accumulate the two amounts and return the result.

Expected output: 14 (print this with console.log)

javascript

function accumulateWithVar(first, second) {
  // Write your solution here
}

console.log(accumulateWithVar(8, 6));

Question 3: Return a Shadowed Value

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)

javascript

function returnShadowedLabel(outer, inner) {
  // Write your solution here
}

console.log(returnShadowedLabel("outside", "inside"));

Question 4: Update a let Counter

Create a let counter from the supplied start value, increment it once, and return it.

Expected output: 10 (print this with console.log)

javascript

function incrementLetCounter(start) {
  // Write your solution here
}

console.log(incrementLetCounter(9));

Question 5: Prefix a Reserved Word

Create a safe identifier string by prefixing the supplied lowercase word with "user_".

Expected output: "user_class" (print this with console.log)

javascript

function prefixReservedWord(word) {
  // Write your solution here
}

console.log(prefixReservedWord("class"));

Question 6: Capture Block Variables

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)

javascript

function captureBlockBindings(outer, inner) {
  // Write your solution here
}

console.log(captureBlockBindings("visible", "inside"));

Question 7: Use a Constant Tax Rate

Store 0.1 in a const variable and return the tax for the supplied price.

Expected output: 8 (print this with console.log)

javascript

function fixedRateTax(price) {
  // Write your solution here
}

console.log(fixedRateTax(80));

Question 8: Update a Constant Object

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)

javascript

function incrementConstantRecord(initial) {
  // Write your solution here
}

console.log(incrementConstantRecord(6));

Question 9: Catch a Strict Assignment Error

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)

javascript

function strictAssignmentError(label) {
  // Write your solution here
}

console.log(strictAssignmentError("test"));

Question 10: Join Greeting Bindings

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)

javascript

function joinGreetingBindings(greeting, name) {
  // Write your solution here
}

console.log(joinGreetingBindings("Welcome", "Ria"));