7A Recap of Programming in JavaScript
7A Recap of Programming in JavaScript
Cosmin E. Oancea
[email protected]
October 2023
Teaching Plan & Format
Semantics of +:
denotes addition when used on numbers, e.g., 5 + 7 = 12,
denotes concatenation when used on strings,
e.g., "Hello " + "World" = "Hello World"
What about on a string and a number: "hello 00" + 7?
Expressions: Evaluation Order & Semantics of +
Evaluation Order: to what is 2 + 3 ∗ 5 evaluated?
(a) 25, as in (2 + 3) ∗ 5, or
(b) 17, as in 2 + (3 ∗ 5) ?
Semantics of +:
denotes addition when used on numbers, e.g., 5 + 7 = 12,
denotes concatenation when used on strings,
e.g., "Hello " + "World" = "Hello World"
What about on a string and a number: "hello 00" + 7?
I equivalent with: "hello 00" + (7).toString()
What is it printed?
Scopes: Syntax, Liveness
A scope is a block of statements enclosed by curly brackets:
let a = 5;
{
let b = a + a ;
a = b * a;
}
What is it printed? 10 70 1
Why var declaration is insane & Grammar
1. What is printed?
l e t anna = ” I am Anna One ! ” ;
{
l e t anna = ” I am Anna Two ! ” ;
co n s o l e . l o g ( anna ) ;
}
anna = anna +
” I r e p e a t : I am Anna One ! ” ;
co n s o l e . l o g ( anna ) ;
Why var declaration is insane & Grammar
1. What is printed?
l e t anna = ” I am Anna One ! ” ;
{
l e t anna = ” I am Anna Two ! ” ;
co n s o l e . l o g ( anna ) ;
}
anna = anna +
” I r e p e a t : I am Anna One ! ” ;
co n s o l e . l o g ( anna ) ;
I am Anna Two !
I am Anna One ! I r e p e a t : I am Anna One !
i f a < 10 a = 100 + a
Sep → ; | newline
else {
...
(1)
b = 10 + a ;
IfStm −→ if ( Exp ) Stm a = 1;
(2) }
IfStm −→ if ( Exp ) Stm else Stm
...
(0)
Stm −→ ;
(1)
Stm −→ Dec Sep
(2)
Stm −→ Exp Sep
(3)
Stm −→ Assgn Sep
(4)
Stm −→ { Block }
(5)
Stm −→ IfStm
...
If Statement: Syntax and Semantics
I want to implement a code with the following semantics:
1. In case the value of variable a is less then 10, I want to update a with the addition
of 100 to whatever a holds;
2. Otherwise: I want to (i) set variable b to the addition of 10 to the value of a, and (ii)
to set variable a to value 1.
Find & fix the bugs in the code snippets below so they conform with the semantics above:
i f a < 10 a = 100 + a
Sep → ; | newline
else {
...
(1)
b = 10 + a ;
IfStm −→ if ( Exp ) Stm a = 1;
(2) }
IfStm −→ if ( Exp ) Stm else Stm
... The condition needs to be enclosed in parentheses
(0)
Stm −→ ; i f ( a < 1 0 ) a = 100 + a e l s e {
(1) b = 10 + a ; a = 1 ;
Stm −→ Dec Sep
(2)
}
Stm −→ Exp Sep
(3)
Stm −→ Assgn Sep
(4)
Stm −→ { Block }
(5)
Stm −→ IfStm
...
If Statement: Syntax and Semantics
I want to implement a code with the following semantics:
1. In case the value of variable a is less then 10, I want to update a with the addition
of 100 to whatever a holds;
2. Otherwise: I want to (i) set variable b to the addition of 10 to the value of a, and (ii)
to set variable a to value 1.
Find & fix the bugs in the code snippets below so they conform with the semantics above:
i f a < 10 a = 100 + a
Sep → ; | newline
else {
...
(1)
b = 10 + a ;
IfStm −→ if ( Exp ) Stm a = 1;
(2) }
IfStm −→ if ( Exp ) Stm else Stm
... The condition needs to be enclosed in parentheses
(0)
Stm −→ ; i f ( a < 1 0 ) a = 100 + a e l s e {
(1) b = 10 + a ; a = 1 ;
Stm −→ Dec Sep
(2)
}
Stm −→ Exp Sep Then statement must end with ; or newline
(3)
Stm −→ Assgn Sep i f ( a < 10) {
(4) a = 100 + a ;
Stm −→ { Block }
(5) } else
Stm −→ IfStm b = 10 + a ;
... a = 1;
If Statement: Syntax and Semantics
I want to implement a code with the following semantics:
1. In case the value of variable a is less then 10, I want to update a with the addition
of 100 to whatever a holds;
2. Otherwise: I want to (i) set variable b to the addition of 10 to the value of a, and (ii)
to set variable a to value 1.
Find & fix the bugs in the code snippets below so they conform with the semantics above:
i f a < 10 a = 100 + a
Sep → ; | newline
else {
...
(1)
b = 10 + a ;
IfStm −→ if ( Exp ) Stm a = 1;
(2) }
IfStm −→ if ( Exp ) Stm else Stm
... The condition needs to be enclosed in parentheses
(0)
Stm −→ ; i f ( a < 1 0 ) a = 100 + a e l s e {
(1) b = 10 + a ; a = 1 ;
Stm −→ Dec Sep
(2)
}
Stm −→ Exp Sep Then statement must end with ; or newline
(3)
Stm −→ Assgn Sep i f ( a < 10) {
(4) a = 100 + a ;
Stm −→ { Block }
(5) } else
Stm −→ IfStm b = 10 + a ;
... a = 1;
Statement a = 1; is not inside the else branch!
For Loop: Syntax and Semantics
The syntax of a for-loop statement is:
1. keyword for, followed by (, followed by
2. an optional assignment (Assgn) followed by ; followed by
3. an optional boolean expression, followed by ; followed by
4. an assignment or expression, followed by
5. ), i.e., close parenthesis, followed by
6. a (block of) statement(s) denoting the loop’s body.
or
f o r ( l e t i = i n i t e x p ; i >= bound exp ; i = i − s t e p e xp ) {
. . . s t a t e m e n t o r body o f s t a t e m e n t s . . .
}
where init exp, bound exp and step exp are expressions that do not contain i:
init exp is used to initialize i for the first iteration of the loop;
bound exp is the maximal/minimal value of i upon which the loop is allowed to
iterate;
step exp is the value by which i is incremented/decremented at each iteration.
While Loop: Syntax and Semantics
The syntax of a while-loop statement is:
1. keyword while
2. followed by a boolean expression enclosed between parentheses, e.g., (i < 100)
3. followed by a (block of) statement(s), i.e., denoting the loop’s body.
Statements
Variable Declaration & Assignment Statement
Scopes
If Statements (Branches)
For and While Loops
Semantics:
The function declaration just implements the intended behavior in terms of some
generic (formal) parameters, but does not executes anything just yet.
The function call instantiates the formal parameters (item 3.) with some evaluated
actual arguments/parameters, and executes the body of the function (item 4.)
The return statement, which has the syntax return Exp; makes sense only inside a
function, and upon execution it terminates the function execution and returns as
the result (of the function call) the evaluated expression.
Why Functions?
Functions are a way to build new functionality that you can re-use multiple times,
Ideally, should be possible to summarize what a function does in a simple way,
Minimizes the number of bugs by eliminating code clones and enabling unit testing,
Allows the user to create customized more-powerful building blocks.
Function Declaration and Function Call
Exp → ...
(5)
Exp −→ name ( Exp , . . . , Exp ) Declaration of a function that computes
... the sum of squares of its two formal
Sep → ; | newline arguments a and b:
Dec → let name f u n c t i o n sumOfSquares ( a , b ) {
Assgn
(1)
−→ name = Exp l e t a sq = a * a ;
(2) l e t b sq = b * b ;
Assgn −→ let name = Exp return a sq + b sq ;
}
(0)
Stm −→ ; Function is called several times:
(1)
Stm −→ Dec Sep
(2) let x = 3;
Stm −→ Exp Sep let y = 4;
(3)
Stm −→ Assgn Sep l e t z = sumOfSquares ( x , y ) ;
(4) / / z = 3 * 3 + 4 * 4 = 25
Stm −→ { Block } l e t w = sumOfSquares ( x + 1 , y + 1 ) ;
... / / w = 4 * 4 + 5 * 5 = 16 + 25 = 41
(8)
Stm −→ return Exp Sep l e t q = sumOfSquares ( z * y , x + 7 ) ;
Block
(1)
−→ / / q = 100 * 100 + 10 * 10 = 10100
(2)
Block −→ Stm Block
(0)
FunDec −→ function name (name,..., name ) { Block }
Longer and Clear Minimizes What You Need to Memorize
A word of advice about how you can keep things simple and clear:
Declare all variables that you are planning to use in a scope at the beginning of
that scope. Also initialize them and remember the type of the value that they are
supposed to hold, e.g., this variable stores a string, another stores an integer, etc.
Use let declaration for variables; var is evil!
Remember that variable and function names are case sensitive: a variable named
anna is different than a variable named Anna.
Explicitly cast values to the intended type, e.g., so that you add two numbers or
concatenate two strings, but you do not add a number to a string.
Always enclose within curly brackets the body of the for/while loop or the bodies of
the then or else branches of an if statement.
Use semicolon after each variable declaration, assignment or expression statement.
Indent properly: all the statements of a scope should be indented by the same
amount – which is a small multiple of the depth at which that scope appears
indent by one tab the statements appearing directly inside a function;
indent by two tabs the statements appearing inside a loop that appears directly
inside a function;
indent by three tabs the statements of the then body of an if appearing inside the
body of a loop that appears directly inside a function . . .
Beware of the grammar rules, e.g., if statements and for and while loops!
Example of proper indentation
i f ( ( i % 2 ) == 0 ) {
/ / i f i i s e ve n add one
sum = sum + 1 ;
} else {
/ / i f i i s odd s u b t r a c t one
sum = sum − 1 ;
}
}
r e t u r n sum ;
}
...
co n s o l e . l o g ( ” R e s u l t o f demo ( 1 0 ) : ” + demo ( 1 0 ) . t o S t r i n g ( ) ) ;
co n s o l e . l o g ( ” R e s u l t o f demo ( 3 3 ) : ” + demo ( 3 3 ) . t o S t r i n g ( ) ) ;
The (Approximate) Grammar that we have derived so far
The lexical tokens are bolded. They include (i) keywords such as return or function,
operators (+,-), punctuation (;,()), values (numbers and strings), and names. The latter two
have a grammar of their own.
(0) (1)
Exp −→ number | string Block −→
(1) (2)
Exp −→ name Block −→ Stm Block
(2)
Exp −→ Exp + Exp
(0)
(3) Stm −→ ;
Exp −→ Exp * Exp
(1)
... Stm −→ Dec Sep
(4) (2)
Exp −→ ( Exp ) Stm −→ Exp Sep
(5) (3)
Exp −→ name ( Exp , . . . , Exp ) Stm −→ Assgn Sep
(4)
Stm −→ { Block }
Sep → ; | newline (5)
Stm −→ IfStm
(1) (6)
IfStm −→ if ( Exp ) Stm Stm −→ ForStm
(2) (7)
IfStm −→ if ( Exp ) Stm else Stm Stm −→ WhileStm
(8)
Stm −→ return Exp Sep
(1)
ForStm −→ for ( Assgn ; Exp ; Assgn ) Stm
(2) (0)
ForStm −→ for ( Assgn ; Exp ; Exp ) Stm FunDec −→ function name (name,..., name )
WhileStm → while ( Exp ) Stm { Block }
Greatest-Common Divisor (GCD) Algorithm
Given two positive integrals a and b,
the GCD of a and b can be computed in the following way:
For as long as a is different than b do the following:
I in the case when a is greater than b assign to a the (positive)
value resulted from subtracting b from a;
I in the other case, assign to b the (positive) value resulted
from subtracting a from b.
After the previous step finishes, a and b hold the value of the
greatest common divisor.
Online Programming:
Implement this textual specification in a function, then add
unit testing for it, i.e., a set of tests that validate the
implementation on some instances for which the result is
known. We will also make the implementation look nice and
readable by using function!
Conclusions