// LAB_9.js
// Task -1: Else statement
function task1() {
let result = prompt("Enter your password:");
if (result === "1234") {
alert("Login completed");
} else {
alert("Login denied");
}
}
// Task -2: Else if statement
function task2() {
let result = prompt("Enter your password:");
if (result === "1234") {
alert("Login completed");
} else if (result === "4321") {
alert("Login completed with alternative password");
} else {
alert("Login denied");
}
}
// Task -3: Comparison operators
function task3() {
let x = 5;
let y = "5";
console.log(x === y); // false
console.log(x == y); // true
}
// Task -4: Logical OR
function task4() {
let password = prompt("Enter your password:");
if (password === "1234" || password === "4321") {
alert("Login allowed");
} else {
alert("Login error:( ");
}
}
// Task -5: Logical AND
function task5() {
let login = prompt("Enter your login:");
let password = prompt("Enter your password:");
if (login === "admin" && password === "1234") {
alert("Login allowed");
} else {
alert("Login error:( ");
}
}
// Task -6: Logical NOT
function task6() {
let password = prompt("Enter your password:");
if (!(password === "1234")) {
alert("Login denied");
} else {
alert("Login allowed");
}
}
// Task -7: If-else example
function task7() {
let a = 50;
let b = 70;
if (a > b) {
console.log(a + b);
} else {
console.log(a * b); // Outputs 3500
}
}
// Task -8: Nested conditions
function task8() {
let a = 50;
let b = 70;
if (a > 10 && b > 70) {
a = a + 10;
b = b * 2;
console.log(a + b);
} else if (a > 40 && b > 40) {
a = a + 50;
b = a * b;
console.log(b); // Outputs 3500
} else {
console.log(0);
}
}
// Task -9: User access function
function task9(login) {
switch (login) {
case "admin":
console.log("Administration, Operations, Statistics, Roles");
break;
case "support":
console.log("Operations, Statistics");
break;
case "user":
console.log("Statistics");
break;
default:
console.log("Invalid login");
}
}
// Task -10: Discount calculation
function task10() {
let check = prompt("Enter the purchase amount:");
check = parseInt(check);
if (check >= 750) {
check = check - (check * 5 / 100);
check = Math.floor(check);
}
console.log(check);
}
// Task -11: For loop sum
function task11() {
let n = 6;
let s = 10;
for (let i = 0; i < n; i++) {
s = s + i;
}
console.log(s); // Outputs 25
}
// Task -12: For loop multiplication
function task12() {
let n = 6;
let p = 1;
for (let i = 0; i < n; i++) {
p = p * i;
}
console.log(p); // Outputs 0
}
// Task -13: Sum purchases over n visits
function task13() {
let n = parseInt(prompt("Enter the number of visits:"));
let total = 0;
for (let i = 0; i < n; i++) {
let s = parseInt(prompt("Enter purchase amount:"));
total += s;
}
console.log(total);
}
// Task -14: getSum function
function getSum(a, b, c) {
return a + b + c;
}
// Main function to execute tasks
function main() {
console.log("Starting Task -1");
task1();
console.log("Starting Task -2");
task2();
console.log("Starting Task -3");
task3();
console.log("Starting Task -4");
task4();
console.log("Starting Task -5");
task5();
console.log("Starting Task -6");
task6();
console.log("Starting Task -7");
task7();
console.log("Starting Task -8");
task8();
console.log("Starting Task -9");
task9("admin"); // Change parameter as needed
console.log("Starting Task -10");
task10();
console.log("Starting Task -11");
task11();
console.log("Starting Task -12");
task12();
console.log("Starting Task -13");
task13();
console.log("Starting Task -14");
console.log(getSum(1, 2, 3)); // Outputs 6
}
// Execute the main function
main();
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容