Skip to content

5.8 Extra Practice

Let Usage

Task 1: Display the even numbers from 0 to 20.

for(var i = 0; i <= 20; i = i + 2){
  console.log(i);
}

The result is as follows:

0
2
4
.
.
.
20

Task 2: Check the following code.

var i = 5;

for(var i = 0; i <= 20; i = i + 2){
  console.log(i);
}
console.log("..........");
console.log(i);

The result is as follows:

0
2
4
.
.
.
20
..........
22

Task 3: Check the following code.

var i = 5;

for(let i = 0; i <= 20; i = i + 2){
  console.log(i);
}
console.log("..........");
console.log(i);

The result is as follows:

0
2
4
.
.
.
20
..........
5

Task 4: Display the elements of the following array.

var users = ["user1", "user2", "user3", "user4"];

for(var user of users){
  console.log(user);
}

The result is as follows:

user1
user2
user3
user4

Task 5: Check the following the code.

var users = ["user1", "user2", "user3", "user4"];
var user = "Sanad";

for(var user of users){
  console.log(user);
}

console.log("...........");
console.log(user);

The result is as follows:

user1
user2
user3
user4
...........
user4

Task 6: Check the following the code.

var users = ["user1", "user2", "user3", "user4"];
var user = "Sanad";

for(let user of users){
  console.log(user);
}

console.log("...........");
console.log(user);

The result is as follows:

user1
user2
user3
user4
...........
Sanad

Task 7: Display the properties of the following object.

var myObj = {
  a: 1,
  b: 2,
  c: 3
}

for(var prop in myObj){
  console.log(prop);
}

The result is as follows:

a
b
c

Task 8: Check the following code.

var myObj = {
  a: 1,
  b: 2,
  c: 3
}

var prop = "d";

for(var prop in myObj){
  console.log(prop);
}

console.log(".........");
console.log(prop);

The result is as follows:

a
b
c
.........
c

Task 9: Check the following code.

var myObj = {
  a: 1,
  b: 2,
  c: 3
}

var prop = "d";

for(let prop in myObj){
  console.log(prop);
}

console.log(".........");
console.log(prop);

The result is as follows:

a
b
c
.........
d

Task 10: Check the following code.

var user = "Jad";
var id = 77;

if(user === "Jad"){
  var id = 54;
}else{
  var id = 44;
}

console.log(id);

The result is: 54

Task 11: Check the following code.

var user = "Jad";
var id = 77;

if(user === "Jad"){
  let id = 54;
}else{
  let id = 44;
}

console.log(id);

The result is: 77

When to declare variables with let keyword?

  1. Use let to declare variables that you want to use only in the block it is defined in.

  2. Use let to declare variables that may be assigned in the code.

Usage examples: 1- loop variables. 2. block variables.

Const Usage

Task 12: Calculate the result of e ** t + 1 for t=0? The value of e constant is 2.7.

const E = 2.7;

console.log(E ** 0 + 1);

The result is: 2

Task 13: Generate 4 users' IDs starting from 45.

const IDSTART = 45;

for (let i = idStart; i < idStart + 4; i++){
  console.log(i)
}

The result is as follows:

45
46
47
48

Task 14: What is the circumference of a circle with diameter 2cm?

const PI = 3.14;

console.log(2 * PI * 1);

The result is: 6.28

Task 15: Check the following code.

const ARR = ["user1", "user2", "user3"];

function searchForUser(user, usersArr){
  if(usersArr.indexOf(user) !== -1){
    console.log("Found in your array.");
  }else if(ARR.indexOf(user) !== -1){
    console.log("Found in the default array.")
  }else{
    console.log("Sorry, the user is not registered with us.")
  }
}

searchForUser("user5", ["user5", "user6", "user88"]);
searchForUser("user1", ["user5", "user6", "user88"]);
searchForUser("user200", ["user5", "user6", "user88"]);

The result is as follows:

Found in your array.
Found in the default array.
Sorry, the user is not registered with us.

Task 16: Refer to task 15; update the default array ARR, and add user899, user795, and user200.

const ARR = ["user1", "user2", "user3"];

function searchForUser(user, usersArr){
  if(usersArr.indexOf(user) !== -1){
    console.log("Found in your array.");
  }else if(ARR.indexOf(user) !== -1){
    console.log("Found in the default array.")
  }else{
    console.log("Sorry, the user is not registered with us.")
  }
}

searchForUser("user5", ["user5", "user6", "user88"]);
searchForUser("user1", ["user5", "user6", "user88"]);
searchForUser("user200", ["user5", "user6", "user88"]);

ARR.push("user899");
ARR.push("user795");
ARR.push("user200");

console.log("................");
searchForUser("user200", ["user5", "user6", "user88"]);

The result is as follows:

Found in your array.
Found in the default array.
Sorry, the user is not registered with us.
................
Found in the default array.

When to declare variables with const keyword?

  1. Use const to declare variables when you want to define a variable that has a constant reference to a value.

  2. Use const to declare variables that should not be re-assigned or re-declared.

Example Usage: 1- Math constants. 2- default arrays or objects(as in task 15-16).