Week 02: Javascript Basics
Lecture
This lecture will focus on basic javascript syntax, statements, and primitive data types.
View the lecture
Homework
- Reading: Read 4.1, 4.4 and the first five sections of chapter 5 (i.e. stop after reading the Array Methods section).
- Assignment: Create a guessing game where the user attempts to guess the correct number (integer). Use
prompt()
to get the user's guesses.- For simplicity, start out by hardcoding the secret number, so it's always the same, i.e.
let secret_number = 4
- Hint: Be sure to validate and convert user input.
prompt()
always returns strings, so you'll need to useparseInt(string)
to get an actual number. Your game should not allow the user to input anything but a valid integer. - Have your game give feedback based on the user's guesses, "correct" for when they're correct, "hot" for when their guess is very close to the secret number, "getting warm" for a bit further, "cool", and "cold" for when they're very far off. Use
console.log()
oralert()
for feedback. - Once this works, use
Math.random()
and otherMath
functions to have the computer pick the secret number instead of hardcoding it. Have the range of numbers be 1-100. Expand your feedback ranges accordingly. - Extra Credit: Create a limit to the number of guesses a user can make, with feedback telling them they've lost when they reach the limit.
- For simplicity, start out by hardcoding the secret number, so it's always the same, i.e.
Send your work by creating a gist, jsbin or similar, or work locally and send me a zip file of all the files that make up your assignment. Send to zachary.schwartz@qc.cuny.edu by Thursday February 15th at 9am.
N.B. Make sure you give the files in your homework assignments proper file extensions (.html
for a web page, .js
for a javascript file)!