# Coding Maze Game Answers

## Page 1

### JavaScript Questions Explained

## Introduction

This page explains answers to JavaScript questions one through five, for the Coding Maze

games.
Questions in the game display in random order. This page covers questions in sequential
order, as the questions are stored in an array, for the game.

Each question was tested with Javascript in file, answers.js.
JavaScript first saves an HTML element from the Web page.
Element `eDebug`

displays the value of `x`

after each question.

var eDebug = document.getElementById('eDebug');

### Question One

The first question's source code follows.
Variable `x`

receives
the value `2`

.
Variable `x`

post decrements to `1`

.
Variable `x`

pre increments back to `2`

.
Therefore the answer equals `2`

.

var x = 2; x--; ++x; eDebug.innerHTML = "1. x:"+x+"<br />";

### Question Two

The second question's source code follows.
Question two uses a ternary operator.
The answer equals `4`

.

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

#### Conditional Ternary Operator

The conditional ternary operator receives three operands.
In the example,
the operands are `(y % 2 == 0), 4`

and `8`

.
The first operand is the condition, `(y % 2 == 0)`

.
The second operand equals `4`

and
the third operand equals `8`

.
If the condition evaluates to true, then
`x`

receives `4`

.
If the condition evalues to false,
then `x`

receives `8`

#### Modulus Operator

The JavaScript modulus operator, represented with `%`

,
returns the remainder after a division.
In this case `y`

equals `12`

.
`12/2`

equals `6`

.
The remainder equals `0`

.
Therefore the condition `(y % 2 == 0)`

returns true.
However if `y`

equaled `13`

,
then `13/2`

equals `6`

with a remainder of `1`

.
In that case then the condition
`(y % 2 == 0)`

would return false.

### Question Three

The third question's source code follows.
The answer equals `8`

.

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

Variable `y`

equals `1`

in decimal format.
In binary format, represented here with just four bits, `y`

equals `0001`

.
JavaScript bitwise operators
treat numbers as
`32`

bit values.
However, for other operations, numbers in JavaScript are stored
as `64`

bit values.

For accuracy sake, with this left bit shift, you can add another `28`

zeros to the left side of `0001`

,
such as `0...0001`

.
For this example the number of binary digits, beyond four,
doesn't matter.

Variable `y`

is represented with four digits in binary, `0001`

.
Move the right most digit left `3`

positions.
The result equals `1000`

in binary, which represents
`8`

in decimal.

### Question Four

Source code for the fourth question follows.
The answer equals `9`

.

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

The bitwise OR

operator, represented with `|`

,
combines bits. If any bit in a number
equals `1`

then the result equals `1`

.
If both bits equal `0`

,
then the result equals `0`

.

Variable `y`

in binary
equals `0001`

. We can ignore the
preceding `28`

zeros, for this example.
Constant `8`

in binary
equals `0100`

.
OR the two numbers together as follows.

0001 0100 ____ 0101

The result `0101`

equals `9`

.

### Question Five

Source code for the fifth question follows.
The answer equals `8`

.

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

The bitwise AND

operator, represented with `&`

,
combines bits. If any bit in a number
equals `0`

then the result equals `0`

.
If both bits equal `1`

,
then the result equals `1`

.

Variable `y`

in decimal equals `12`

,
in binary
equals `1100`

. We can ignore the
preceding `28`

zeros, for this example.
Constant `9`

in binary
equals `1001`

.
AND the two numbers together as follows.
The result `1000`

equals `8`

.

1100 1001 ____ 1000

### JavaScript Output:

### Play Coding Maze Games

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