Tarkibga o'tish

1-bo'lim: Asoslar

↑ Mundarijaga qaytish

1. Hello World

Ekranga matn chiqarish.

JS

console.log("Hello, World!");
PHP
echo "Hello, World!";
Python
print("Hello, World!")

2. Ikki sonni qo'shish

Ikki sonni qabul qilib, yig'indisini qaytaradi.

JS

const add = (a, b) => a + b;
PHP
function add($a, $b) {
    return $a + $b;
}
Python
def add(a, b):
    return a + b

3. O'zgaruvchilarni almashtirish (swap)

Ikki o'zgaruvchi qiymatini almashtirish.

JS

[a, b] = [b, a];
PHP
[$a, $b] = [$b, $a];
Python
a, b = b, a

4. Aylana yuzasi

Radius bo'yicha aylana yuzasi (Ο€rΒ²).

JS

const circleArea = r => Math.PI * r ** 2;
PHP
function circleArea($r) {
    return M_PI * $r ** 2;
}
Python
import math

def circle_area(r):
    return math.pi * r ** 2

5. Salomlashish

Ismni qabul qilib, salomlashish matnini qaytaradi.

JS

const greet = name => `Salom, ${name}!`;
PHP
function greet($name) {
    return "Salom, $name!";
}
Python
def greet(name):
    return f"Salom, {name}!"

6. Selsiy β†’ Farengeyt

Haroratni Selsiydan Farengeytga aylantirish.

JS

const cToF = c => c * 9 / 5 + 32;
PHP
function cToF($c) {
    return $c * 9 / 5 + 32;
}
Python
def c_to_f(c):
    return c * 9 / 5 + 32

7. Juft yoki toq

Son juftligini tekshirish.

JS

const isEven = n => n % 2 === 0;
PHP
function isEven($n) {
    return $n % 2 === 0;
}
Python
def is_even(n):
    return n % 2 == 0

8. Ikki sondan kattasi

JS

const max2 = (a, b) => a > b ? a : b;
PHP
function max2($a, $b) {
    return $a > $b ? $a : $b;
}
Python
def max2(a, b):
    return a if a > b else b

9. Uch sondan kattasi

JS

const max3 = (a, b, c) => Math.max(a, b, c);
PHP
function max3($a, $b, $c) {
    return max($a, $b, $c);
}
Python
def max3(a, b, c):
    return max(a, b, c)

10. Sonning ishorasi

Musbat / manfiy / nol ekanini aniqlash.

JS

const sign = n => n > 0 ? "musbat" : n < 0 ? "manfiy" : "nol";
PHP
function sign($n) {
    return $n > 0 ? "musbat" : ($n < 0 ? "manfiy" : "nol");
}
Python
def sign(n):
    return "musbat" if n > 0 else "manfiy" if n < 0 else "nol"

11. Kabisa yil (leap year)

JS

const isLeap = y => (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
PHP
function isLeap($y) {
    return ($y % 4 === 0 && $y % 100 !== 0) || $y % 400 === 0;
}
Python
def is_leap(y):
    return (y % 4 == 0 and y % 100 != 0) or y % 400 == 0

12. Sekundlarni HH:MM:SS formatiga

JS

const fmtTime = s => {
  const h = Math.floor(s / 3600);
  const m = Math.floor((s % 3600) / 60);
  const sec = s % 60;
  return [h, m, sec].map(x => String(x).padStart(2, "0")).join(":");
};
PHP
function fmtTime($s) {
    return sprintf("%02d:%02d:%02d", intdiv($s, 3600), intdiv($s % 3600, 60), $s % 60);
}
Python
def fmt_time(s):
    h, rem = divmod(s, 3600)
    m, sec = divmod(rem, 60)
    return f"{h:02d}:{m:02d}:{sec:02d}"

13. Oddiy kalkulyator

Ikki son va amal bo'yicha natija.

JS

const calc = (a, op, b) => {
  switch (op) {
    case "+": return a + b;
    case "-": return a - b;
    case "*": return a * b;
    case "/": return b !== 0 ? a / b : "0 ga bo'lib bo'lmaydi";
  }
};
PHP
function calc($a, $op, $b) {
    return match ($op) {
        "+" => $a + $b,
        "-" => $a - $b,
        "*" => $a * $b,
        "/" => $b != 0 ? $a / $b : "0 ga bo'lib bo'lmaydi",
    };
}
Python
def calc(a, op, b):
    table = {
        "+": a + b,
        "-": a - b,
        "*": a * b,
        "/": a / b if b != 0 else "0 ga bo'lib bo'lmaydi",
    }
    return table[op]

14. BMI (tana massasi indeksi)

kg / mΒ².

JS

const bmi = (kg, m) => +(kg / m ** 2).toFixed(1);
PHP
function bmi($kg, $m) {
    return round($kg / $m ** 2, 1);
}
Python
def bmi(kg, m):
    return round(kg / m ** 2, 1)

15. Chegirma narxi

Narx va chegirma foizi bo'yicha yakuniy narx.

JS

const discount = (price, pct) => price - price * pct / 100;
PHP
function discount($price, $pct) {
    return $price - $price * $pct / 100;
}
Python
def discount(price, pct):
    return price - price * pct / 100