Title: Loops and Errors Assignment
Duration: 1 hr approx
Creator: Brandi Butler (Inspired by GA-ATX's lab - thank you!)
Topics: Errors, While loops, For Loops
Forkandclonethis repository as we practiced in class- Once you've cloned to your local machine, open
solutions.js - As you come up with solutions to each section, write them in
solutions.js- Remember, you can execute the code with the command:
node solutions.js - For text (or non-code) answers, write them in multi-line comments
- Remember, you can execute the code with the command:
- Once you've either finished the assignment OR put in 3 hours of work turn in the assignment
git add -Agit commit -m "Adding my homework"git push origin master- Go to your Github repository and click "New Pull Request"
- Comment with any questions you ran into!
So far, we have learned about the following topics
- Errors and debugging
- Declaring variables with
var - Boolean expressions
- While loops
- For loops
Let's get some extra practice with them. We will continue to use these concepts throughout the course.
Work through the following prompts in turn:
Cut and paste the following code into your text editor (yep, it's the "Cheers" song lyrics). The code is broken -- there are three errors. You might already see the errors, but --
Run the code and read the error message in the terminal. Using information from the error message (line numbers, etc.), debug the code and make it work.
At the same time, write a comment below the code that specifies what type of error it was. Example, if you get this:
Write
// SyntaxError: missing ) after argument listCut and paste:
console.log("Making your way in the world today takes everything you've got.");
console.log("Taking a break from all your worries, sure would help a lot.");
console.log("Wouldn't you like to get away?");
console.log("Sometimes you want to go");
console.log("Where everybody knows your name,");
console.lo("and they're always glad you came.");
console.log("You wanna be where you can see,";
console.log("our troubles are all the same");
console.log("You wanna be where" "everybody knows");
console.log("Your name.");Make it so you do not get any more errors!
... and arithmetic
By just looking at the following expressions, determine in your mind whether or not each will evaluate to true or false.
Remember what you learned about truthy-ness and falsey-ness!
999 > 999999 == 999999 != 999-5 >= -4100 <= -10020 + 5 < 581 / 9 == 99 != 8 + 1"abc" == "abc""a" == "b"
Now how about some trickier ones...
5 == 55 === 55 == "5"5 === "5"50"a"""[]!!true
- What is the difference between:
the assignment operator =
and the equality operator ==
?
- What is the difference between:
this equality operator ==
and this equality operator ===
?
Write a while loop that will log in the console
'Ginger chocolate honey patties'
1000 times. You can test it out with a smaller number first, such as 10, and when that works, use 1000.
Make sure you do not run an infinite loop! If you do, kill the process in your Terminal. Oops!
Write another while loop that counts from 0 to 1000 and will log in the console the current loop number.
=> 0
=> 1
=> 2
=> 3
etc.
Write another while loop that prints a message to the console and interpolates the current loop number. Make it count from 0 to 1000.
=> "Current loop number is: 0"
=> "Current loop number is: 1"
=> "Current loop number is: 2"
=> "Current loop number is: 3"
NOTE: Interpolation is a way of using variables in your string. You can use a set of backticks ` instead of regular quotes, and between the two backticks, put whatever strings and variables you want. For variables, specify them with a dollar sign and curly braces.
var name = "Rhonda"
console.log("Hello", name);- Write a for loop that counts from 0 to 100 and console.logs each number.
- Write another for loop that counts from 7 to 635 (no more, no less!), and console.logs each number.
- Declare a variable
var start = 0 - Declare a variable
var LIMIT = 100 - Write a for loop that counts from the value of
startto the value oflimit, using those variables in the control panel of the loop.
Test the loop thoroughly!
BONUS QUESTION: Why did we declare LIMIT in all caps? What does that all-caps style usually signal (as opposed to the regular camelCasing)?
- Think of something in real life, or nature, or wherever / whenever that displays looping behavior.
Use a for loop to model the looping behavior of that thing.
Where does the loop begin? Where should it end? Does it simply count from one number to another? Or does it change or mutate data?
Protip: Don't have a vivid imagination? Borrow some from your neighbor!
Congrats! There is no need to submit this lab. We will be going over the answers together soon.
If you have extra time and want more to do, use your research skills (Google-fu) to find out more about:
- Using a nested loop (a loop inside another loop)
- A full list of falsey values
- Write a for loop that skips every other number
- What are for..in loops used for?
