/unit-2-practice-assessment

Study Guide for SE Unit 2 Assessment

GNU General Public License v3.0GPL-3.0

Unit 2 Practice Assessment - JavaScript Foundations

Short Responses

  1. The function declaration comes after the variable declaration in the code below. What will happen when it this code runs?
let greeting = "Hello!"

function sayItLoud() {
  console.log(`${greeting}!!!`)
}

sayItLoud();
  1. Okay... so why does the code below throw an error?
sayItLoud();

function sayItLoud() {
  console.log(`${greeting}!!!`)
}

let greeting = "Hello"
  1. Mmhmmm... so what about this😰. What does the following code log? Why?
sayItLoud();
function sayItLoud() {
  console.log(`${greeting}!!!`)
}
var greeting = "Hello"
  1. Why does the following block of code throw an error? How can we fix this without changing the variable declaration keyword?
const isSunny = true;

if (isSunny) {
  let status = 'No umbrella needed!';
} else {
  let status = 'Better wear a raincoat!'
}

console.log(status);

tags: #variableScope key: "let is block scoped"

  1. Why does the following block of code NOT throw an error?
const isSunny = true;

if (isSunny) {
  var status = 'No umbrella needed!';
} else {
  var status = 'Better wear a raincoat!'
}

console.log(status);

tags: #variableScope key: "variables declared with var are not block scoped"

  1. In JavaScript, we can declare variables with var, let, and const. What are the differences between each? Be sure to comment on how each declaration impacts the scope, reassignment, and hoisting of variables.

  2. Where does the following code throw an error? What type of error? Why?

let a = b;

console.log(a);

tags: #variableDeclaration key: "the variable b is not defined."

  1. What is the value of y after this code runs? Explain why this is the case.
let x = 1;
let y = x;
x += 1;

console.log(x);
console.log(y);

tags: #mutability key: "y points to the integer 1, a primitive data type", "reassigning x has no effect on y"

  1. What does the following code log? Explain why?
let a = {goals: "Enmanuel"};
let b = a;
a.goals = "Cielo";
console.log(b.goals);

tags: #mutability key: "b points to the same object referenced by a"

  1. Where does the following code throw an error? What type of error? Why?
const fellows = 'Itzel Carmen';
fellows = 'Itzel Carmen Shemar';

console.log(fellows);

tags: #variableDeclaration #mutability #primitives key: "variables declared with const cannot be reassigned"

  1. Wait, why doesn't the code below throw an error?! 🧐 What does this demonstrate?
const fellows = ['Itzel', 'Carmen'];
fellows.push('Shemar');

console.log(fellows);

tags: #mutability #objects key: "fellows was not reassigned", "JavaScript objects are mutable"

  1. What does the following code log? Why?
let greeting = "What's up?";

function say() {
 return `${greeting}! 🤟🏿`
}

console.log(say);

tags: #functionExecution #functionArguments key: "function was not invoked"

  1. What does the following code log? What does this say about JavaScript function arguments?
function shoutOut() {
  console.log(`The flyest person in the room is Carmen!`);
}

shoutOut('Devonte');

tags: #functionExecution #variableScope key: "arguments are optional", "extraneous arguments are ignored"

  1. What happens if a function is invoked with fewer arguments than there are parameters? Include a code snippet to illustrate.

tags: #functionExecution #functionArguments key: "missing parameters are set to undefined"

  1. What about extra parameters? How can we access them and use them to our advantage? Illustrate by writing a function that takes any number of integers as arguments and returns their sum.

Example:

sum(1, 2, 10); // 13
sum(5); // 5
sum(100, 200, 800, 1, 1, 1); // 1103;

tags: #functionExecution key: the array-like argument object, the ES6 rest parameter syntax

  1. What does the following code log? Why?
let bestCoder = 'Laura';

function shoutOut() {
  bestCoder = 'Motun';
}

shoutOut();
console.log(`The best coder in the room is ${bestCoder}`.);

tags: #variableScope key: "functions have access to variables defined in their local scope and all surrounding scopes"

  1. What does the following code log? Why?
let theGoat = 'Reuben';

function shoutOut() {
  let theGoat = `Maya`;
  console.log(`${theGoat} is the hardest working person in the room.`);
}

shoutOut();
console.log(`${theGoat} is also the hardest working person in the room.`);

tags: #variableScope key: "lexical scope"

  1. What does the following code log? Why?
let theCEO;

function hire(theCEO) {
  theCEO = 'Mark';
}

hire(theCEO);
console.log(`I have no doubt that ${theCEO} will be running a company of his own very soon. I just hope that he will hire me when I need a job.`);

tags: #variableScope key: "variable shadowing" the function declarations creates a new local variable declaration for its parameter, theCEO."

  1. What does it mean to pass by value? For what data types does JavaScript pass by value? Use a code snippet to illustrate.

tags: #mutability #functionExecution key: "primitive data types are passed by value"

  1. What does it mean to pass by reference? For what data types does JavaScript appear to pass by reference? Use a code snippet to illustrate.

tags: #mutability #functionExecution key: "JavaScript objects are passed by reference"

  1. Explain why the first code snippet below throws an error, but the second does not.
10 + a;        // ReferenceError: a is not defined
let a = {};
10 + a.x;      // Evaluates to NaN

tags: #objects key: "undefined properties of objects return undefined"

  1. What is the relationship between JavaScript Objects and JavaScript Arrays? We say a JavaScript Object is empty if it has no properties of its own. We say that a JavaScript Array is empty if it has no elements. How do we check for emptiness of JavaScript Objects? How do we check for emptiness of JavaScript Arrays. Use code snippets to illustrate your answer.

tags: #objects #arrays key: "JavaScript arrays are objects", "use .length to check for array emptiness", "use Object.keys(object) to check for object emptiness"

Problem Set

  1. Write a function that takes a string, doubles every consonant character in the string, and returns the result as a new string. The function should not double vowels ('a','e','i','o','u'), digits, punctuation, or whitespace.

Examples:

doubleConsonants('String');          // "SSttrrinngg"
doubleConsonants('Hello-World!');    // "HHellllo-WWorrlldd!"
doubleConsonants('July 4th');        // "JJullyy 4tthh"
doubleConsonants('');                // ""
  1. Write a function that takes a string and returns the string reversed.

Examples:

reverse('Test'); // "tseT"
reverse('anotherTest'); // "tseTrehtona"
reverse('factor In spaces'); // "secaps nI rotcaf"
reverse(''); // ""
  1. Write a function takes an integer year as an argument and returns a boolean based on whether the year is a leap year. Note: Every year that is evenly divisible by 4 is a leap year, except every year that is evenly divisible by 100, unless the year is also evenly divisible by 400.

Examples:

isLeapYear(1997); // false
isLeapYear(1996); // true
isLeapYear(1900); // false
isLeapYear(2000); // true
  1. Write a function that combines two arrays into one new array. The new array should start with an item from the first array argument, then an item from the second array argument, and then alternates. You can assume the two input arrays are the same length.

Example:

combineArrays([1, 2, 3], [4, 5, 6]);      // [1, 4, 2, 5, 3, 6]
combineArrays(['a', 'b'], ['x', 'y']);    // ['a', 'x', 'b', 'y']
combineArrays([true], [false]);           // [true, false]
  1. Write a function that returns an object that shows how many times each vowel shows up in a sentence (vowels are 'a', 'e', 'i', 'o', and 'u'). The returned object should have exactly 5 key-value pairs, one for each vowel (the value shold be the number of times that vowel appears in the sentence).

Example:

countVowels('Hello World');                                     // {'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0}
countVowels('The quick brown fox jumped over the lazy dog');    // {'a': 1, 'e': 3, 'i': 1, 'o': 4, 'u': 2}
countVowels('Over the Marcy Lab School');                       // {'a': 2, 'e': 1, 'i': 0, 'o': 3, 'u': 0}
  1. Write a function that takes a sentence string as an argument, and returns that string with every occurrence of a "number word" — 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' — converted to its corresponding digit character.

Example:

wordToDigit('Please call me at five five five one two three four. Thanks.');
// "Please call me at 5 5 5 1 2 3 4. Thanks."
  1. For a game of Dungeons & Dragons, each player starts by generating a character they can play with. This character has, among other things, six abilities; strength, dexterity, constitution, intelligence, wisdom and charisma. These six abilities have scores that are determined randomly. You do this by rolling four 6-sided dice and record the sum of the largest three dice. You do this six times, once for each ability.

Your character's initial hitpoints are 10 + your character's constitution modifier. You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down.

Write a random character generator function that follows the rules above.

For example, the six throws of four dice may look like:

  • 5, 3, 1, 6: You discard the 1 and sum 5 + 3 + 6 = 14, which you assign to strength.
  • 3, 2, 5, 3: You discard the 2 and sum 3 + 5 + 3 = 11, which you assign to dexterity.
  • 1, 1, 1, 1: You discard the 1 and sum 1 + 1 + 1 = 3, which you assign to constitution.
  • 2, 1, 6, 6: You discard the 1 and sum 2 + 6 + 6 = 14, which you assign to intelligence.
  • 3, 5, 3, 4: You discard the 3 and sum 5 + 3 + 4 = 12, which you assign to wisdom.
  • 6, 6, 6, 6: You discard the 6 and sum 6 + 6 + 6 = 18, which you assign to charisma. Because constitution is 3, the constitution modifier is -4 and the hitpoints are 6.

Your function should take one argument, a name. It should generate random attribute values using Math.random(). The function should return a JavaScript Object that contains all of the above attributes.

Example:

characterGenerator("Elminster");

/*
{
  name: "Elminster",
  strength: 10, // randomly generated
  dexterity: 8, // randomly generated
  constitution: 12, // randomly generated
  intelligence: 14, // randomly generated
  wisdom: 12, // randomly generated
  charisma: 18, // randomly generated
  hitpoints: 11 // 10 + ((12-10)/2)
}

*/