Coding Maze Game Answers

Page 3

JavaScript Questions Explained

Introduction JavaScript Output JavaScript answers.js

Introduction

Questions and answers were verified with JavaScript before becoming part of the Coding Maze Games. This page includes the JavaScript source code used to test each question. You may also see the JavaScript Output.

JavaScript File answers.js with Comments

"use strict"
var eDebug = document.getElementById('eDebug');
eDebug.innerHTML = "<h3>JavaScript Results</h3>";
eDebug.innerHTML += "<p>You may download, keep, or just view <a href='javascript/answers.js' title='answers.js'>answers.js</a> for the verifiable results.</p>"
var x = 2; x--; ++x;
eDebug.innerHTML += "1. x:"+x+"<br/>";

x = 0;
var y = 12;
x = (y % 2 == 0) ? 4 : 8;
eDebug.innerHTML += "2.  x:"+x+"<br/>";

x = 0;
y = 1;
x = y << 3;
eDebug.innerHTML += "3.  x:"+x+"<br/>";

x = 0;
y = 1;
x = y | 8;
eDebug.innerHTML += "4.  x:"+x+"<br/>";

x = 0;
y = 12;
x = y & 9;
eDebug.innerHTML += "5.  x:"+x+"<br/>";

x = 0;
y = -4;
x = y >> 2;
eDebug.innerHTML += "6. x:"+x+"<br/>";

x = 0;
y = {age:20,name:"Roy"}
x = y.name;
eDebug.innerHTML += "7. x:"+x+"<br/>";

y = { age:20, name:"Roy", f:function(){ this.age++; }};
y.f();
x = y.age;
eDebug.innerHTML += "8. x:"+x+"<br/>";

x = 0;
y = [1,2,3];
x = typeof y;
eDebug.innerHTML += "9. x:"+x+"<br/>";

y = "Chuck";
x = typeof y;
eDebug.innerHTML += "10. x:"+x+"<br/>";

/**
 * SECOND SET OF QUESTIONS
 */
eDebug.innerHTML += "<br/><br/>Second set of ten questions<br/>";
y = 5;
x = y + 12;
eDebug.innerHTML += "11. x:"+x+"<br/>";

y = 8 * 5;
x = y + 5;
eDebug.innerHTML += "12. x:"+x+"<br/>";

y = "Hello World!";
x = y;
eDebug.innerHTML += "13. x:"+x+"<br/>";

y = 8;
//y += 9;
x = y;
eDebug.innerHTML += "14. x:"+x+"<br/>";

y = 30;
x = y % 2;
eDebug.innerHTML += "15. x:"+x+"<br/>";

y = function(){return 22;}
x = y();
eDebug.innerHTML += "16. x:"+x+"<br/>";

y = "Chuck";
x = y.indexOf('k');
eDebug.innerHTML += "17. x:"+x+"<br/>";

y = [3,1,2];
x = y.sort();
eDebug.innerHTML += "18. x:"+x+"<br/>";

y = 2;
x = y + "1";
eDebug.innerHTML += "19. x:"+x+"<br/>";

y = 3;
x = y == 3;
eDebug.innerHTML += "20. x:"+x+"<br/>";

/**
 * THIRD SET OF QUESTIONS
 */
eDebug.innerHTML += "<br/><br/>Third set of ten questions:<br/>";
y = 3;
x = y;
x <<= 2;
eDebug.innerHTML += "21. x:"+x+"<br/>";

x = Math.round(22.5);
eDebug.innerHTML += "22. x:"+x+"<br/>";

x = Math.pow(2, 4);
eDebug.innerHTML += "23. x:"+x+"<br/>";

x = Math.sqrt(16);
eDebug.innerHTML += "24. x:"+x+"<br/>";

x = Math.sin(90 * Math.PI / 180);
eDebug.innerHTML += "25. x:"+x+"<br/>";

eDebug.innerHTML += "<br/>Questions 26,27, and 28 demonstrate rounding to the nearest two decimal places:<br/>";

eDebug.innerHTML += "<br/>x = Math.sin(90 * Math.PI / 270);<br/>";
x = Math.sin(90 * Math.PI / 270);
eDebug.innerHTML += "26. x:"+x+"<br/>";

eDebug.innerHTML += "<br/>Math.round(x * 100).toFixed(2);<br/>"
x = Math.round(x * 100).toFixed(2);
eDebug.innerHTML += "27. x:"+x+"<br/>";

eDebug.innerHTML += "<br/>x /= 100;<br/>"
x /= 100;
eDebug.innerHTML += "28. x:"+x+"<br/><br/>";

x = Math.min(2,9,5,-12);
eDebug.innerHTML += "29. x:"+x+"<br/>";

x = Math.max(2,9,5,-12);
eDebug.innerHTML += "30. x:"+x+"<br/>";

/**
 * Fourth SET OF QUESTIONS
 */
eDebug.innerHTML += "<br/><br/>Fourth set of ten questions:<br/>";

// === means both type and value must match.
y = 12;
x = y === "12";
eDebug.innerHTML += "31. x:"+x+"<br/>";

y = 12;
x = y === 12;
eDebug.innerHTML += "32. x:"+x+"<br/>";

// var is not block scoped.
x = 9;
var i = 0;
// Redeclare x inside the block.
// Changes x outside the block.
for (i = 0; i < 4; i++){var x = i;}
// Externally declared x has changed:
eDebug.innerHTML += "33. x:"+x+"<br/>";

// Let keyword block scoped x
x = 9;
var i = 0;
for(i = 0; i < 4; i++){let x = i;}
// Externally declared x is unchanged.
eDebug.innerHTML += "34. x:"+x+"<br/>";

// Can change property on a constant object.
const j = {siamese:"pretty", cats:"awesome"};
j.siamese = "expensive";
eDebug.innerHTML += "35. j.siamese:"+j.siamese+"<br/>";

// ES5
y = "   Code all night.";
x = y.trim();
eDebug.innerHTML += "36. x:"+x+"<br/>";

y = ["Persian","Bombay","Feral"];
x = Array.isArray(y);
eDebug.innerHTML += "37. x:"+x+"<br/>";

x = '';
y = ["Ocicat","Snow Leopard","Jaguar"];
eDebug.innerHTML += "38. ";
y.forEach(f1);
function f1(cat){x += cat+',';}
eDebug.innerHTML += "x: "+x+"<br/>";


y = [4,9,-3,12];
x = y.filter(f2);
function f2(value) { return value > 4;}
eDebug.innerHTML += "<br/>39. x:"+x+"<br/>";

y = [4,9,-3,12];
x = y.reduce(f3);
function f3(sumSoFar,value){return sumSoFar + value;}
eDebug.innerHTML += "40. x:"+x+"<br/>";

JavaScript Output Displays Below

Play Coding Maze Games

Play all six levels of coding mazes. Each level includes at least two different scenes.

Learn 3D Programming Learn 3D Programming
Copyright © 2015 Seven Thunder Software. All Rights Reserved.