In today lab, we will get familiar with the fundamental building blocks of programming.
We can run Javascript in our browser, VSCode, and on our webpage.
week2-basic-javacript/
|- monday.js
|- tuesday.js
|- wednesday.js
|- thursday/
|-- draft.txt
To run your program (js file) in VScode, we will need node js
. Don't worry, we will learn it later. For now, simply use in your terminal
node <path_to_file>.js
For example, if you are currently at c/haha/huhu
, and your monday.js
is at c/haha/huhu/inHere
, you should
cd inHere
node monday.js
or
node inHere/monday.js
You could turn things in to comments so that the compiler will ignore and not run
// exercise 1
var x = 3;
// a thousand line...
// later ...
// exercise 5
var x = 4;
If your try to run this file, error will happen. review lecture on how to comment code to ignore previous solution and keep working on new one
Create a piece of program that print the calculated area of circle
on to the console.
pi
, and assign it the value 3.14
in one statement.pi
to the console.radius
, and assign the value 5
to it in one statement.area
which is calculated by pi
and radius
to get the area of a circleHint : the area of a circle is pi times the radius squared and in JavaScript the **exponentiation operator** is `**`.
string + value + string ...
The area of a circle given the radius ... (m) is ... (m2)
radius
to 7
and log the result again using template literal. string ${value} string
- String concatenation: "string + value + string ... "
- Template literal: `string ${value} string`
Turn every statement into true
by compare the given expression with your expected result
. You must use ===
to compare. Remember NOT
to use ==
To gain the most out of this exercise try :
predict
before compile codeHint
: To make a comment in JS , use //anything here
. A comment will not be run by node.1 + 3
1 + "3"
true && true && false
1 + 3 === 4; //true
// expected result of 1 + 3 is number 4, so compare it with number 4 lead to true
1 + "3" === "13"; //true
// expected result of 1 + 3 is string 13, so compare it with string 13 lead to true
true && true && false === false; //true
// expected result of boolean true AND true is true
// expected result of true AND false is false
// so true AND true AND false is false, since no value could be both true and false.
// hence, the expression will result as false, if we use false to compare it will finnally True
10 + 24
"10" + "24"
"Hello" + " " + 2021
1 + 2 * 3
(1 + 3) ** 2
1 / 0
6 % 2
5.5 % 2
Number("123")
typeof(1 + "")
5 == "5"
5 === "5"
8 != 8.0
8 !== 8.0
"true" === true
4 <= 4.0
7 >= 7
true && true
true && false
true || true
false || true
!true
!false
false && (true || true)
false && true || true
Create a piece of program that detect if a number is positive, negative or zero
x
and assign it with a number of your choice.x
is equal to 0 or a positive/negative number in an "user-friendly" way.Example
5 is a positive number 0 is zero 3 is a negative number
a
, b
, c
, and assign them with values in the range 0 to 99 of your choice. Write code to print out the smallest and biggest number between them. Example:var a = 1;
var b = 4;
var c = 80;
//your code here
1 is the smallest number, and 80 is the biggest number between a = 1,b = 4, and c = 80
Create ONE piece of program that log to console a chain of number, which ordered based on some condition.
a
and b
(0 < a
< b
< 10).for
loop to print all numbers from a
to b
inclusively:a < b
,a > b
.var a = 1,
b = 5;
Output: 1 2 3 4 5
var a = 7,
b = 4;
Output: 7 6 5 4
Create ONE piece of program that log to console sum of all number from start to end.
x
and y
(0 < x
< y
< 999). Write a loop to calculate the sum of numbers from x
to y
inclusively. Print out the result in an user-friendly way.Example: Sum of numbers from 1 to 800 is: ...
Create a piece of program that log to console sum of all digits in a given number
1
,2
,3
,4
in number 1234
Example input: 1024 Example output: Sum of digits of 1024 is 7
Create functions to better manage your code
your name
, and return your name
in the middle of a sentence. "console.log" the result.conditional
, moving the logic in your previous CONDITIONAL
exercise into this functiona
,b
and c
function conditional(*something here*){
//something here
console.log("something here")
}
//execute a function and give value as argument example
conditional("something here")
... is the smallest number, and ... is the biggest number between a = ...,b = ..., and c = ...
seriesOfNumber
a
and b
as 2 argumentsLoops : Part 1
so that its result will be changing according to value of arguments when function is executed// Example
function seriesOfNumber(a, b) {
// Your code here
}
seriesOfNumber(8, 5); // In the console: 8 7 6 5
Loops : Part 2
and Loops : Part 3
getLeapYears(start, end)
that returns a list of leap years from the year start
to the year end
inclusively. (A year is a leap year if its number is exactly divisible by 4 and is not exactly divisible by 100. A year is always a leap year if its number is exactly divisible by 400).// Example
getLeapYears(1899, 2001);
// 1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000
Create a piece of software that
a
with value 111 and variable b
with value 999temp
a
and b
a
and b
before and after swapping in an "user-friendly" wayRocket (optional): Swap values without declare another variable. Meaning only a
and b
is use
Math.random()
To get a random number, we will use a built-in function of JavaScript called Math.random()
. This function will return a random between 0 and less than 1 (i.e. 0.99999...).
Let's try it!
console.log(Math.random());
If you hit Run a few times (or copy and paste this line multiple times), you should see a different result each time.
So how do we turn these results into usable numbers? If we multiply them by 3 (or any number), we will get results ranging from 0 (inclusive) and 3 (exclusive):
If we just take the integer part of each number, then the results will be among 0, 1 and 2 (but not 3)! And we can use Math.floor()
to round down these floating numbers to their integer values. For example, the result of Math.floor(5.95)
will be 5
.
Combining Math.random()
and Math.floor()
, we can get random integers between 0
and some number - 1
.
console.log(Math.floor(Math.random() * 3)); // => will output a number between 0 and 2
floor
, ceil
and round
Use this as guide for your rounding. Beware of accuracy issue relating to number in Programming, this is an advance topic that you should just kind in mind that it is exist. For now, don't bother go to deep
Method | Definition | Result |
Math.floor(1.93) | round down to an integer | 1 |
Math.ceil(1.93) | round up to an integer | 2 |
Math.round(1.93) | round to the nearest integer | 2 |
Math.round(1.49) | round to the nearest integer | 1 |
Now that you have learned the basics of JavaScript, let's use it to create a Magic-8-Ball game! This game will return a random YES/NO answer every time you run it.
function play() {
// more code here
}
0
and 1
and assign it to a variable.const randomNumber = // enter your code
When executed, the fuction will:
randomNumber
is 0
.randomNumber
is 1
.Will be available the day after