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.

What You'll Learn

Requirement :

week2-basic-javacript/
|- monday.js
|- tuesday.js
|- wednesday.js
|- thursday/
  |-- draft.txt

Tip 1 : running / compiling your code

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

Tip 2 : Telling compiler to ignore some code

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

Requirement :

Create a piece of program that print the calculated area of circle on to the console.

Instruction :

Hint : the area of a circle is pi times the radius squared and in JavaScript the **exponentiation operator** is `**`.
The area of a circle given the radius ... (m) is ... (m2)
- String concatenation: "string + value + string ... "
- Template literal: `string ${value} string`

Requirement

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 :

Example:

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

Part 1 : Operators expressions

Part 2 : Comparison expressions

Part 3 : Logical expressions

Requirement

Create a piece of program that detect if a number is positive, negative or zero

Instruction

Example

 5 is a positive number
 0 is zero
 3 is a negative number

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

Part 1 : List of number in order asc/desc

Create ONE piece of program that log to console a chain of number, which ordered based on some condition.

Instruction

var a = 1,
  b = 5;
Output: 1 2 3 4 5
var a = 7,
  b = 4;
Output: 7 6 5 4

Part 2 : Sum all from start to end

Create ONE piece of program that log to console sum of all number from start to end.

Instruction

Example: Sum of numbers from 1 to 800 is: ...

Part 3 : Sum of digits

Create a piece of program that log to console sum of all digits in a given number

Instruction

Example input: 1024
Example output: Sum of digits of 1024 is 7

Requirement

Create functions to better manage your code

Instruction

Problem 1

Problem 2

Problem 3

Problem 4

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 = ...

Problem 5

// Example
function seriesOfNumber(a, b) {
  // Your code here
}

seriesOfNumber(8, 5); // In the console: 8 7 6 5

Problem 6 : ROCKET

Problem 7: ROCKET

Get all leap years

// 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

Requirement

Create a piece of software that

Rocket (optional): Swap values without declare another variable. Meaning only a and b is use

Overview of 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

Rounding number table with 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
}
const randomNumber = // enter your code

When executed, the fuction will:

Will be available the day after