a set of instructions that tell a computer what to do.
the rules of valid format and combinations of instructions. aka syntax. Just as the English language.
variables are like simple boxes where you can store any of your stuff in.
a group of words, numbers, and operators that performs a specific task.
example:
a = b * 2;
variables:
a
,b
literal values:
2
operators:
=
,*
an expression is any reference to a variable or value, or a set of variable(s) and value(s) combined with operators.
example:
a * b
4 expressions in total:
b
is a variable expression
2
is a literal value expression
b * 2
is an arithmetic expression of multiplication
a = b * 2
is an assignment expression
interpreted code is read on the fly, from top to bottom, line by line, execution happens immediately.
compiled code is when all the translation to commands happens first, then the program is executed.
the JavaScript engine actually compiles the program on the fly then immediately runs the compiled code.
Consider: var a = 1;
Write the code to:
console.log(a);
alert(a);
Code challenge:
Ask the user's name with a prompt message "Please, type your username"
and store it in a variable username
, then log the value in the console.
Solution:
var username = prompt('Please, type your username');
console.log(username);
JavaScript has both u___ and b___ operators, and one special t___ operator
unary, binary, ternary
Name the types of operators you know, and give some basic examples.
assignment:
=
,+=
,-=
,*=
,/=
comparison:
==
,!=
,===
,!==
,>
,<
,>=
,<=
arithmetic:
+
,-
,/
,*
,++
,--
logical:
&&
,||
,!
number, string, boolean
when values are converted to a different type, for example, converting a string to a number, or a number to string.
implicit coercion is done automatically
example:
var number = 10;
// number is converted to a string
console.log(number);
explicit coercion is when you want to convert values from one type to another
example:
var number = 10;
// I want to convert number to a string
number = String(number);
// number is a string already, no need to converted
console.log(number)
single-line comment:
// this is a single line comment
multiline comment:
/* this is
a multiline
comment */
comments describe the hows and whys of something implemented in code. They help to document things in a way other team members and your future self can understand better your code.
comments are one effective way to write more readable code.
weak typing
variables can hold only the type they are defined with, for example:
int number = 10
variables can hold any type, for example:
var a = 10;
a = 'Isaac'
constants are placed on top of a program.
constants are capitalized separated with an underscore
_
, example:TAX_RATE
use keywork
const
only availble for ES6.
var amount = 100;
{
amount = amount * 2;
console.log(amount)
}
yes, it's called
general block
24. Write a block of code to validate if a variable number
is less than 10
to log one digit
, log two digits
otherwise.
Solution:
if (number < 10) {
console.log('one digit');
} else {
console.log('two digits');
}
while
solution:
var i = 0;
while(i <= 9) {
console.log('number ->', i);
i = i + 1;
};
do-while
solution:
var i = 0;
do {
console.log('number ->', i);
i = i + 1;
} while(i <= 9)
for
solution:
for (var i = 0; i <= 9; i = i + 1) {
console.log('number ->', i);
}
initialization clause
conditional test clause
update clause
a named block of code that performs a specific task. It's called (executed) by its name.
Solution:
function sum (a, b) {
return a + b;
}
a collection of variables as well as the rules for how those variables are accessed by name.
Consider:
function outer() {
var a = 1;
function inner() {
var b = 2;
}
inner();
}
outer();
true
false
1.1 Create a function calculateAreaOfACircle
that receives radius
as parameter. Use a constant PI
equal to 3.14159
. Avoid the temptation of using the Math
library.
Solution:
// create the constat PI here
const PI = 3.14159;
// create your function here
function calculateAreaOfACircle(r) {
return PI * (r * r);
}
// Do NOT touch this code.
console.log('Expect area of a circle with radius = "3" to be "28.27431" ->', calculateAreaOfACircle(3) == 28.27431)