July 11, 2023

Javascript Lesson-4

Scopes

What is scope?

Scope o'zgaruvchilarni o'zini tutish maydoni

Scope'ni 5xil turi bor

  • Global
  • Block
  • Local or Function
  • Lexical
  • Module

var and let

  • var - global-scoped
  • let - block-scoped

masala:

var a = 3
{
var a = 10
{ var a = 15
{
a= 40
}
}
}
console.log(a)

code yozyotganimiz global scope hisoblanadi, qavsni ichidagilar block spcope hisoblanadi.

console'dag9i qiymat oxiri 40 chiqadi

let a = 3 // globalniy scope'da 3ga teng
{
let a = 5 // block scope'da 5ga teng
console.log(a) // 5 chiqadi blockni ichidagi qiymat sifatida
}
console.log (a) // 3 chiqadi global scope sifatida
// elon qilmasak, bitta ozgaruvchi bo'lib, ozgaradi, lkn elon qilsak block va global scopelar alohida bo'ladoi

agar biz varda qilayotgan bo'lsak, umumiy oxiridagi qiymatini ozgartirib qoyadi, let block va global scopelarni ajratib beradi

FOR LOOP

for (initializer; condition; iterator) {

// statements

}

bu for'ning strukturasi

masalan:

(misollar orqali tushuntiriligan)

// for (let i = 1; i <= 10; i++) {//   console.log(i);// }
// for (let i = 1; i <= 100; i++) {//   console.log(i);// }
// for (let i = 1; i <= 100; i++) {//   if (i % 2 == 1) {//     console.log(i);//   }// } // 100 times
// for (let i = 1; i <= 100; i += 2) {//   console.log(i);// } // 50 times
// for (let i = 2; i <= 100; i += 2) {//   console.log(i);// }
// for (let i = 10; i >= 1; i--) {//   console.log(i);// }
// for (let i = 100; i >= 1; i--) {//   console.log(i);// }
// for (let i = 99; i >= 1; i -= 2) {//   console.log(i);// }
// for (let i = 100; i >= 2; i -= 2) {//   console.log(i);// }

// let sum = 0;// let mul = 1;
// for (let i = 1; i <= 10; i++) {//   sum += i;//   mul *= i;// }
// console.log(sum);
// console.log(mul);
// let sum = 0;// let mul = 1;
// for (let i = 3; i <= 9; i += 3) {//   sum += i;//   mul *= i;// } // 3 times
// let i = 1;
// for (;;) {//   if (i > 10) {//     break;//   }//   if (i % 3 === 0) {//     sum += i;//     mul *= i;//   }//   i++;// } // 10 times
// console.log(sum);// console.log(mul);
// console.log(i);
// let sum = 0;
// for (let i = 0; i < 10; i++, sum += i);
// console.log(sum);

simple:
for (let i = 1; i <= 10; i++) {

console.log(i);

}

E'lon qilishlarsz:

let j = 1; // initilizer for (; j <= 10; j += 1) {

console.log(j);

}

conditionsz:

for (let j = 1; ; j += 1) {

console.log(j); if (j > 10) { break; } // condition

}

iteratorsz:
for (let j = 1; ; j += 1) {

console.log(j); if (j > 10) {

break;

} // condition

}

expressionsz:
let j = 1; // initilizer for (;;) { if (j > 10) {

break;

} // condition console.log(j); j += 1; // iterator

}

bodysz:

for (let i = 0; i <= 10; i++, console.log(i));

while

while synatxis:

while (condition) { // code block to be executed }

let count = 1;

while (count < 10) { console.log(count); count +=2; }

masalan:

let i = 1;

while ( i > 10){ //condition togri bolayotgan bo'lsa, shuni ichidagi code'lar ishlaydi)

console.log(i );

i++;

}

initializition va iteratorlari har xil joyda boladi

masalalar:

N va K butun musbat sonlari berilgan. Faqat ayirish va qo'shish amallarini ishlatib N sonini K soniga bo'lgandagi qoldiq va butun qismini aniqlovchi programma tuzilsin.

let n = 55;

let k = 9;

let count= 0;

bizni iteratsiyamiz 9 dan kichik son paydo bolguncha davom etadi. Iteratsiya 9 ga teng va undan katta bolyotgan bolsagina davom etayotgan bolishi kerak

while (n > = k ){

count ++ n - = k

}

console.log(n)

console.log(count)

manashuni nomi BOLIB BUTUNNI VA QOLDIQNI ANIQLASH

2 sonining qandaydir darajasini bildiruvchi n butun soni berilgan (n > 0). n = 2^k ni aniqlovchi programma tuzilsin.

Input: 128

Output: 7

let n =32;

count = 0

while ( n !=1){
n = n / 2;

}

console.log(count);

DO WHILE;

do { statement;

} while (expression);

let count = 0;

do { console.log(count);

count++;

} while (count < 5)

bu strukturasi;

let i = 1;

do {
console.log (i);

i ++;

} while (i > 10);

PROJECT

const realPasword = "12345";

let password;

let count = 0

do {
password = prompt ("enter the password")

count ++;

} while (password !== reaslpassword);

alert ( "Siz passwordni " + count + "martada topdingiz!")

BREAK keyword

for (let i = 0; i < 5; i++) {

console.log(i);

if (i == 2) {

break; }

}

bunda console'ga 0, 1, 2 chiqadi sababi break orqali codemiz toxtatilgan

while

let i = 0; while (i < 5) {

i++; console.log(i); if (i == 3)

{

break;

}

}

0, 1, 2 , 3 console'ga chiqadi 4ga otkazmaydi

continue keyword

for (let i = 0; i < 10; i++) {

if (i % 2 === 0)

{

continue;

}

console.log(i);

}

while

for (let i = 0; i < 10; i++) {

if (i % 2 === 0)

{

continue;

} console.log(i);

}

break — codelari toxtadi va iteratsiya ham toxtaydi, continue esa ozidan keyingi codelarni ishlatmaydi lekin iteratsiayadavom etadi