Created
May 7, 2025 11:36
-
-
Save devitfro/3283677245c1b3d8c91321a2779790be to your computer and use it in GitHub Desktop.
js_homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<p>hello world!</p> | |
<!--<script src="app.js"></script>--> | |
<script> | |
// Task 1 | |
// Запросіть у користувача число, зведіть це число в 2 ступінь та виведіть на екран. | |
let userNumber = Number(prompt('Enter your number')); | |
let power = 2; | |
let resultNumber = toPower(userNumber, 2); | |
alert(`The result of your number ${userNumber} to power ${power}: ${resultNumber}`); | |
function toPower(number, power) { | |
return number ** power; | |
} | |
// Task 2 | |
// Запросіть у користувача 2 числа та виведіть середнє арифметичне цих чисел. | |
let number1 = Number(prompt('Enter your number 1')); | |
let number2 = Number(prompt('Enter your number 2')); | |
let avg = getAvg(number1, number2); | |
alert(`The average of ${number1} and ${number2} is ${avg}`); | |
function getAvg(number1, number2) { | |
return (number1 + number2) / 2; | |
} | |
// Task 3 | |
// Запросіть у користувача довжину сторони квадрата та виведіть його площу. | |
let side = Number(prompt('Enter side of square')); | |
alert(`The area: ${getArea(side)}`); | |
function getArea(side) { | |
return side * side; | |
} | |
// Task 4 | |
// Користувач вводить значення a і b для формули a*x+b=0, а програма підраховує і виводить значення x. | |
let a = Number(prompt('Enter a')); | |
let b = Number(prompt('Enter b')); | |
alert(`The x: ${getX(a, b)}`); | |
function getX(a, b) { | |
return (-b + Math.sqrt(b * b - 4 * a * a)) / (2 * a); | |
} | |
// Task 5 | |
// Запросіть у користувача його ім'я та виведіть у відповідь «Привіт, (його ім'я)!». | |
let userName = prompt('Enter your name'); | |
function sayHello(name) { | |
alert(`Hello, ${userName}!`); | |
// return `Hello, ${name}!`; | |
} | |
sayHello(userName); | |
// Task 6 | |
// Запросіть у користувача його рік народження, | |
// підрахуйте скільки йому років і виведіть результат. Поточний рік вкажіть у коді як константу. | |
let year = Number(prompt('Enter your birthday year')); | |
const currentYear = 2025; | |
console.log(`Your age is ${getAge(year)}`); | |
function getAge(year) { | |
while (year > currentYear || year < 1900) { | |
console.log("Not valid year"); | |
year = Number(prompt('Enter your birthday year')); | |
} | |
return currentYear - year; | |
} | |
// Task 7 | |
// Запросіть у користувача радіус кола та виведіть його площу. 2pir | |
let r = Number(prompt('Enter r')); | |
const PI = Math.PI; | |
let result = getAreaCircle(r); | |
console.log(`The area: ${result}`); | |
function getAreaCircle(r) { | |
return PI * r * r; | |
} | |
// Task 8 | |
// Користувач вказує обсяг флешки у ГБ. Програма має підрахувати скільки файлів розміром 820 МБ вміститься на флешку. | |
const MB = 820; | |
const GB = 1024; | |
let res = getFilesCount(); | |
console.log(res); | |
function getFilesCount() { | |
let flashCapacity = Number(prompt('Enter capacity of fleshka')); | |
while ( flashCapacity <= 0) { | |
alert("Not valid capacity"); | |
flashCapacity = Number(prompt('Enter capacity of fleshka')); | |
} | |
let capacityMB = flashCapacity * GB; | |
return Math.floor(capacityMB / MB); | |
} | |
// Task 9 | |
// Запросіть у користувача ціле число і виведіть у відповідь парне або непарне. | |
// В завданні використовуйте логічні оператори і не використовуйте if та switch. | |
let number = Number(prompt('Enter number')); | |
let result = getResult(number); | |
console.log(result); | |
function getResult(number) { | |
return (number % 2 === 0 && 'Even') || 'Odd'; | |
} | |
// Task 10 | |
// Реалізуйте калькулятор. Користувач вводить два числа, а програма виводить результати дій +, –, *, / цих чисел. | |
let number1 = Number(prompt('Enter your number 1')); | |
let number2 = Number(prompt('Enter your number 2')); | |
function calculator(number1, number2) { | |
console.log(`Sum: ${number1 + number2}`); | |
console.log(`Diff: ${number1 - number2}`); | |
console.log(`Mult: ${number1 * number2}`); | |
if (number2 == 0) { | |
console.log("Not correct to divide by zero"); | |
} | |
console.log(`Div: ${number1 / number2}`); | |
} | |
calculator(number1, number2); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment