Javascript - Conditionals and loops
Learning goals
Debugging in the browser
Operators
Equality operator
Relational operator
Logical operator
Conditional
If, else, else if sentence
Loops
Forloop
Flipped classroom videos
Teacher instruction
Undervis i debugging
Husk ikke at sammenligne jer med hinanden for meget!! Sammenlign dig selv med hvor du var for 2 uger siden. Hvis du har rykket dig der, så tillykke. Det går som det skal
Today will be some real workout exercises
Work in the groups you worked in yesterday
11:30 to see some of the solutions
Der er mange opgaver. Det er ikke forventet at i laver dem alle! Vi skal lige lære jeres niveau og hvor mange opgaver i kan køre igennem
Peer instruction
Question 1 - 2 min
What will the following code log to the console?
truefalse"true""false"Syntax error
None of the above
Question 2 - 1 min
What will the following code log to the console?
over 2000over 3000exactly 3000Something elseSyntax error
None of the above
Question 3 - 2 min
What numbers will the following code log to the console?
4,3,2,1,00,1,2,3,43,2,1,0Continues to decrease infinite from 4
None of the above
Syntax error
Debuggin in the browser
To start debugging in the browser first we need to create a Debug Configuration
First click the dropdown with the text Current File to the left of the green play icon. Now click Edit Configurations...

Create a new configuration by clicking the + and then select JavaScript Debug

Now copy the url where the project is running (when you click the browser in the html file)


Operators
Equality operator
Note the two different uses of the equals sign: A single equals sign (=) is used to assign a value to a variable. A triple equals sign (===) is used to compare two values (see Equality Operators).
Equality operators
Equality operators are used for checking if to values are equal each other
Equality
==Inequality
!=Identity / strict equality
===(preferred)Checks both type and value
Non-identity / strict inequality
!==(preferred)
How does this work in practice?
Relational operator
Relational operators are used when checking how two values relate to each other
Greater than operator
>Greater than or equal operator
>=Less than operator
<Less than or equal operator
<=
Logical operator
Logical operators are used for the code to take decisions
AND
&&- both values on the right side and the left side has to betruefor an and sentence to betrueOR
||- either values on the right side or the left side has to betruefor an or sentence to betrue
📝 Exercise 1 - level 1
Together in the group with pen and paper write the answer for the following question. No computer!
2 === 2true && falsefalse || true3 > 47 > 11 && 'a' === 'b' || truetrue === true && false === true
Conditionals
Like humans, computer programs make decisions based on the information given to them. Conditionals are a way of representing these decisions in code (remember, you saw this in a previous exercise!)
For example:
In a game, if the player has 0 lives, then the game is over
In a weather app, if rain is forecast, a picture of rain clouds is shown
The most common type of conditional is the if statement.
If sentence
An if statement runs some code if a condition is met. If the condition is not met, then the code will be skipped.
The code in paratheses - e.g. (isHappy) - is the condition. The condition can be any expression. The following are all valid conditions:
An if statement runs code when a condition is met. What if the condition is not met? Sometimes you want to run an alternative bit of code.
If else sentence
An if...else statement also runs code when the condition is not met.
What if there is more than one condition you want to handle in your program? For example, what if you can be confused as well? You can use else if to handle multiple conditions.
Else if sentence
📝 Exercise 2 - level 1
In your group explain what these lines of code does
📝 Exercise 2.1 - level 1
Create a variable called balance. If balance is larger than 10000 log out I am rich 💰. Otherwise log out I am poor 🪙
📝 Exercise 3 - level 2
Create a small program that gives you a message depending on your mood! It should:
Use a variable called
moodLog
"Good job, you're doing great!"ifmoodis"happy"Log
"Every cloud has a silver lining"ifmoodis"sad"Log
"Beep beep boop"ifmoodis a numberLog
"I'm sorry, I'm still learning about feelings!"ifmoodis anything else
Write the text to the webpage using document.write
Loops
When we're writing programs, we often find that we want to repeat a bit of code over and over, or repeat it but change something about it each time. To save ourselves from writing all that code, we can use a loop. JavaScript has two kinds of loops, a while loop and a for loop. I will only cover the for loop.
for loop
The for loop is similar to a while loop, but with a more specialized syntax. Programmers invented the for loop when they realized they were always doing the same three things: creating loop counter variables (like i above), incrementing them by some amount, and checking that they're less than a value.
The for loop syntax has special places for each of those three things. Here's the same loop as the first while loop above, as a for loop:

📝 Exercise 5 - level 1
Print out
Hello World3 timesUse a loop to print the numbers from 1 to 10
Use a loop to print a
*10 timesUse a loop to print the numbers starting from 5 to -5
Use a loop to print every third number from 5 to 30
📝 Exercise 6 - teachers and students
Duration: 20 min
In the following exercise one group will randomly be selected to be teachers and the other group will be students
In groups of two people prepare a small 5 minute lecture. The lecture should explain the topic of for loops any way you like. That might be with a small slideshow or it might be with code, thats up to you.
As teachers present the 5 minute lecture
As students ask good interesting questions
📝 Exercise 7 - level 2
Using a for loop console.log a random number of hashtags (#)
If randomNumber is 7 then 7 hashtags should be logged
📝 Exercise 8 - level 2
Using a for loop console.log the following output 👇
📝 Exercise 8.1 - level 2
Using a for loop and conditionals console.log the following
📝 Exercise 9 - level 2
Create a for loop that logs out each indvidual character in a string
With the last character log out "This is the last character". With the string Copenhagen the following output is expected👇
Tip: To make this work, read this article
📝 Exercise 9.1 - level 3
Create a for loop that logs out each indvidual character in a string
With the middle character log out "This is the middle character". With the string Hello the following output is expected👇
Tip: To make this work, read this article
What is the word has an even number of characters?
📝 Exercise 10 - Fizzbuzz - level 3
You need to write a program that prints the numbers from 1 to 100 such that:
📝 Exercise 11 - level 3
You can have a for loop in a for loop. See if you can print the following patterns to the console

Last updated