Fix each of the following variable declarations in a console -- some are syntactically invalid, some are disobey style guidelines, and some are just weird.
var "animal" = "monkey";
var "monkey" = animal;
var x= 15;
var y =10;
var var = "huh?";
var true = false;
var isTenEven = 10 % 2 = 0;
Perform the following in the console:
firstName and assign your first name to it.lastName, and assign your last name to it.fullName and assign your full name to it by using
the above variables.For each of the following code blocks, use a whiteboard to reason about
what the value of x is supposed to be on the last line. Once you have
arrived at a conclusion that you are comfortable with, enter the lines into a
console and check your answer. Was your hypothesis correct? If not, ensure
that you understand why (talk with a classmate, or ask for help).
var x = 5;
x + 10;
x; // => ???
var x = 17;
x = (x + 1) / 2;
x * 4;
x; // => ???
var x = 5;
var y = 20;
x = y;
y = y + 7;
x; // => ???
var x = 10;
var y = 5;
x = (x * 4) - 3;
x + 17;
x = x + y;
x; // => ???
Write a function called counter that, when invoked, always returns a number
that is one more than the previous invocation. For instance:
function counter() {
// TODO: your code here
}
counter(); // => 1
counter(); // => 2
counter(); // => 3
// etc.
HINT: You'll need a variable for this. Where should the variable be declared?
All of the following exercises involve augmenting the guessMyNumber function.
In a previous module you wrote a function called guessMyNumber that
simulated a guessing game: the idea is that the function picks a random
number between 0 and 5, and you invoke the function with your guess -- if
you and the function are thinking of the same number, you win! Otherwise, the
function informs you that your guess was incorrect. A version of this game
might look like this (the randInt function is included for convenience):
function guessMyNumber(n) {
if (n > 5) {
return "Out of bounds! Please try a number between 0 and 5.";
} else if (n === randInt(5)) {
return "You guessed my number!";
}
return "Nope! That wasn't it!";
}
function randInt(n) {
return Math.floor(Math.random() * (n + 1))
}
Read and test both of the functions in your console (copy or write the code
into your main.js file, and then invoke the functions from the console) and
affirm that you understand how they work; then, answer the following
questions:
0 and 5. We can think of 5 as
the upper bound of the guess. How many times is the upper bound
repeated? What if we wanted to change the upper bound to 6? How many
changes would be required?upperBound to hold the upper bound, and then
reference it instead of the number 5. If you were asked to change the
upper bound to some other number (e.g. 7), you should only have to make
one change.guessMyNumber so that if the guess is incorrect, guessMyNumber
includes the correct guess in its output, e.g. "Nope! The correct number was: X" (where X would have been the correct number).At present, the guessing game picks a new random number every time it is "played" (invoked). Now that you know how to make information persistent between function invocations, change the guessing game so that it picks a random number once and allows you to guess until you get the correct answer.
It would be really cool if, after the answer was guessed, the message included the number of guesses it had taken to find the answer; for example, "You guessed my number in 3 guesses."
Tangent Problem: What happens if you get the number right on the first try? Does it say, "You guessed my number in 1 guesses."? If so, perhaps the wording should be different? Some better ideas are:
Implement a way to limit the number of guesses that can be made so that a player loses after exceeding the limit.
Keep track of a high score (the lowest number of guesses) between games, and, when the correct number has been guessed in a record number of times, include in the message something that indicates that a new high score has been set.
Whenever a player wins, increase the difficulty by increasing the
upperBound; whenever a player loses, decrease the difficulty by
decreasing the upperBound.
Implement a high/low hinting system to tell the the user that the guess
is either too high or too low. You may want to increase the upperBound on
the guess.
There is an optimal way to play this game that works like this, given upperBound as the upper bound, lowerBound as the lower bound, and guess as the guess:
Initialize the starting values:
Execute guessMyNumber with guess:
Your task is to write a function that implements the above algorithm
to play the game on your behalf. The first thing that you will need to
do is create another version of guessMyNumber that returns output that
will be easier for another function to work with, e.g. use 1 for too
high, -1 for too low, 0 for correct.
Relative to upperBound, how many guesses does it take on average to guess correctly?
Some recommendations:
console.log on
MDN
and use it to help with debugging.