Variables & Identifiers - Exercise 1
Question 1: Store and Update a Score Completed
Declare a mutable score initialized from the argument, add 5 to it, and return the updated score.
Expected output: 17 (print this with console.log)
function updateStartingScore(startingScore) {
// Write your solution here
}
console.log(updateStartingScore(12));function updateStartingScore(startingScore) {
let score = startingScore;
score += 5;
return score;
}
console.log(updateStartingScore(12));Question 2: Build an Identifier Completed
Return the supplied non-empty words joined in camelCase, with the first word lowercase and the second capitalized.
Expected output: "userName" (print this with console.log)
function makeCamelIdentifier(first, second) {
// Write your solution here
}
console.log(makeCamelIdentifier("user", "name"));function makeCamelIdentifier(first, second) {
return first.toLowerCase() + second[0].toUpperCase() + second.slice(1).toLowerCase();
}
console.log(makeCamelIdentifier("user", "name"));Question 3: Reassign a Quantity Completed
Store the initial quantity in a let variable, replace it with the new quantity, and return it.
Expected output: 9 (print this with console.log)
function replaceQuantity(initial, replacement) {
// Write your solution here
}
console.log(replaceQuantity(4, 9));function replaceQuantity(initial, replacement) {
let quantity = initial;
quantity = replacement;
return quantity;
}
console.log(replaceQuantity(4, 9));Question 4: Use Descriptive Bindings Completed
Store the rectangle's width and height in local variables and return its area.
Expected output: 28 (print this with console.log)
function areaFromBindings(dimensions) {
// Write your solution here
}
console.log(areaFromBindings({"width":4,"height":7}));function areaFromBindings(dimensions) {
const width = dimensions.width;
const height = dimensions.height;
return width * height;
}
console.log(areaFromBindings({"width":4,"height":7}));Question 5: Create a Full Name Completed
Use a camelCase variable named fullName to join the two names with one space, then return it.
Expected output: "Ada Lovelace" (print this with console.log)
function joinFullName(firstName, lastName) {
// Write your solution here
}
console.log(joinFullName("Ada", "Lovelace"));function joinFullName(firstName, lastName) {
const fullName = firstName + " " + lastName;
return fullName;
}
console.log(joinFullName("Ada", "Lovelace"));Question 6: Calculate with a Constant Completed
Declare a constant for 60 minutes per hour and return the number of minutes for the given hours.
Expected output: 180 (print this with console.log)
function hoursToMinutes(hours) {
// Write your solution here
}
console.log(hoursToMinutes(3));function hoursToMinutes(hours) {
const minutesPerHour = 60;
return hours * minutesPerHour;
}
console.log(hoursToMinutes(3));Question 7: Read a Hoisted Declaration Completed
Read a var variable before its declaration, assign the supplied label, and return an array with its earlier typeof result and assigned value.
Expected output: ["undefined","ready"] (print this with console.log)
function readHoistedVar(label) {
// Write your solution here
}
console.log(readHoistedVar("ready"));function readHoistedVar(label) {
const before = typeof message;
var message = label;
return [before, message];
}
console.log(readHoistedVar("ready"));Question 8: Keep a Block's Value Local Completed
Create an outer value and a separate block-scoped value from the arguments. Return the outer value after the block.
Expected output: "outside" (print this with console.log)
function keepOuterValue(outer, inner) {
// Write your solution here
}
console.log(keepOuterValue("outside", "inside"));function keepOuterValue(outer, inner) {
let value = outer;
{ let value = inner; }
return value;
}
console.log(keepOuterValue("outside", "inside"));Question 9: Find a Binding's Type Completed
Store the input in a local variable and return its typeof result.
Expected output: "boolean" (print this with console.log)
function typeOfBinding(input) {
// Write your solution here
}
console.log(typeOfBinding(false));function typeOfBinding(input) {
const value = input;
return typeof value;
}
console.log(typeOfBinding(false));Question 10: Compare Two Stored Codes Completed
Store both inputs in local variables and return whether they are strictly equal.
Expected output: false (print this with console.log)
function compareStoredCodes(first, second) {
// Write your solution here
}
console.log(compareStoredCodes("007", 7));function compareStoredCodes(first, second) {
const firstCode = first;
const secondCode = second;
return firstCode === secondCode;
}
console.log(compareStoredCodes("007", 7));