JavaScript Assessment

Q1. Which operator returns true if the two compared values are not equal?

Q2. How is a forEach statement different from a for statement?

Q3. Review the code below. Which statement calls the addTax function and passes 50 as an argument?

function addTax(total) {
  return total * 1.05;
}

Q3 How would you use this function to find out how much tax should be paid on $50?

(Version 2, possibly an updated version)

function addTax(total) {
  return total * 1.05;
}

Q4. Which statement is the correct way to create a variable called rate and assign it the value 100?

Q5. Which statement creates a new object using the Person constructor?

Q6. When would the final statement in the code shown be logged to the console?

let modal = document.querySelector('#result');
setTimeout(function(){
    modal.classList.remove('hidden);
}, 10000);
console.log('Results shown');

Q7. You've written the code shown to log a set of consecutive values, but it instead results in the value 5, 5, 5, and 5 being logged to the console. Which revised version of the code would result in the value 1, 2, 3 and 4 being logged?

for (var i = 1; i <= 4; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 10000);
}

Q8. How does a function create a closure?

Q9. Which statement creates a new function called discountPrice?

10. What is the result in the console of running the code shown?

var Storm = function () {};
Storm.prototype.precip = 'rain';
var WinterStorm = function () {};
WinterStorm.prototype = new Storm();
WinterStorm.prototype.precip = 'snow';
var bob = new WinterStorm();
console.log(bob.precip);

Q11. You need to match a time value such as 12:00:32. Which of the following regular expressions would work for your code?

Q12. What is the result in the console of running this code?

'use strict';
function logThis() {
  this.desc = 'logger';
  console.log(this);
}
new logThis();

Q13. How would you reference the text 'avenue' in the code shown?

let roadTypes = ['street', 'road', 'avenue', 'circle'];

Q14. What is the result of running this statement?

console.log(typeof(42));

Q15. Which property references the DOM object that dispatched an event?

Q16. You're adding error handling to the code shown. Which code would you include within the if statement to specify an error message?

function addNumbers(x, y) {
  if (isNaN(x) || isNaN(y)) {
  }
}

Q17. Which method converts JSON data to a JavaScript object?

Q18. When would you use a conditional statement?

Q19. What would be the result in the console of running this code?

for (var i = 0; i < 5; i++) {
  console.log(i);
}

Q20. Which Object method returns an iterable that can be used to iterate over the properties of an object?

Q21. After the following code, what is the value of a.length?

var a = ['dog', 'cat', 'hen'];
a[100] = 'fox';

Q22. What is one difference between collections created with Map and collections created with Object?

Explanation

Map.prototype.size returns the number of elements in a Map, whereas Object does not have a built-in method to return its size.

Q23. What is the value of dessert.type after executing this code?

const dessert = { type: 'pie' };
dessert.type = 'pudding';

Q24. 0 && hi

Q25. Which of the following operators can be used to do a short-circuit evaluation?

Q26. Which statement sets the Person constructor as the parent of the Student constructor in the prototype chain?

Q27. Why would you include a "use strict" statement in a JavaScript file?

Q28. Which Variable-defining keyword allows its variable to be accessed (as undefined) before the line that defines it?

Q29. Which of the following values is not a Boolean false?

Q30. Which of the following is not a keyword in JavaScript?

Q31. Which variable is an implicit parameter for every function in JavaScript?

Q32. For the following class, how do you get the value of 42 from an instance of X?

class X {
  get Y() {
    return 42;
  }
}

Q33. What is the result of running this code?

sum(10, 20);
diff(10, 20);
function sum(x, y) {
  return x + y;
}

let diff = function (x, y) {
  return x - y;
};

Q34. Why is it usually better to work with Objects instead of Arrays to store a collection of records?

Explanation

Records in an object can be retrieved using their key which can be any given value (e.g. an employee ID, a city name, etc), whereas to retrieve a record from an array we need to know its index.

Q35. Which statement is true about the "async" attribute for the HTML script tag?

Q36. How do you import the lodash library making it top-level Api available as the "_" variable?

Q37. What does the following expression evaluate to?

[] == [];

Q38. What is the name of a function whose execution can be suspended and resumed at a later point?

Q39. What will this code print?

var v = 1;
var f1 = function () {
  console.log(v);
};

var f2 = function () {
  var v = 2;
  f1();
};

f2();

Q40. Which statement is true about Functional Programming?

Q41. Your code is producing the error: TypeError: Cannot read property 'reduce' of undefined. What does that mean?

Q42. How many prototype objects are in the chain for the following array?

let arr = [];

Q43. Which of the following is not a unary operator?

Q44. What type of scope does the end variable have in the code shown?

var start = 1;
if (start === 1) {
 let end = 2;
}

Q45. What will the value of y be in this code:

const x = 6 % 2;
const y = x ? 'One': 'Two';

Q46. Which keyword is used to create an error?

Q47. What's one difference between the async and defer attributes of the HTML script tag?

Q48. The following program has a problem. What is it?

var a;
var b = (a = 3) ? true : false;

Q49. Which statement references the DOM node created by the code shown?

<p class="pull">lorem ipsum</p>

Q50. What value does the code return?

let answer = true;
if (answer === false) {
  return 0;
} else {
  return 10;
}

Q51. What is the result in the console of running the code shown?

var start = 1;
function setEnd() {
  var end = 10;
}
setEnd();
console.log(end);

Q52. What will this code log in the console?

function sayHello() {
  console.log('hello');
}

console.log(sayHello.prototype);

Q53: Which collection object allows unique value to be inserted only once?

Q54. What two values will this code print?

function printA() {
  console.log(answer);
  var answer = 1;
}
printA();
printA();

Q55. For the following class, how do you get the value of 42 from "X" ?

class X {
  get Y() {
    return 42;
  }
}
var x = new X();

Q56. How does the forEach() method differ from a for statement?

Q57. What will be logged to the console?

'use strict';
function logThis() {
  this.desc = 'logger';
  console.log(this);
}
new logThis();

Q58. Which choice is an incorrect way to define an arrow function that returns an empty object?

Q59. Why might you choose to make your code asynchronous?

Q60. Which expression evaluates to true?

Q61. Which choice is a valid variable name?

Q62. Which method cancels event default behavior?