c2
// const arr = [1, 2, 3, 4];
// // array destructuring
// const [a, b] = arr;
// console.log(a); 
// console.log(b); 
// console.log("Object destructuring");
// const obj = {
//     id:12,
//     name:"Dhruvin",
//     address:"Bordi"
// }
// const{id,name,...rest}=obj;
// console.log(id); 
// console.log(name);
// console.log(rest);
//  //--------------------------------------------------
//  class Emp{
//     name="dhruvin";
//     id=21;
//     salary=200000
//     displayInfo(){
//         console.log("id= "+this.id+"\n"+this.name+"\n"+this.salary);
//     }
// }
// const emp=new Emp;
// emp.displayInfo();
// //-------------------------------------------------
// function emp(id,name,address="Goa"){
//     console.log("id :"+id+" \nname : "+ name +"\naddress : "+address); 
// }
// emp(21,"Dhruvin","Bordi");
// emp(22,"Tom");
// console.log("----------------")
// function total(...num){
//    return num.reduce((total,num)=>total+num,0)
// }
// console.log( total(1,2,3,4,5,6));
// //--------------------------------------------------
// try {
//     const pi=3.14
//     pi=33
//     console.log(pi);
// } catch (error) {
//     console.log(error.name+"="+error.message);
// }
// //-------------------------------------------------
// function meth()
// {
// const pi=3.14;
// pi=333;
// console.log(pi);
// }
// function meth1() {
//     meth();
// }
// function meth2()
// {
//     try {
//         meth1();
//     } catch (error) {
//         console.log(error.name+"="+error.message);
//     }
// }
// meth2();
// //----------------------------------------
// function meth()
// {
// const pi=3.14;
// pi=333;
// console.log(pi);
// }
// try {
//     meth();
// } catch (error) {
//     console.log(error.name+"="+error.message);
// }
// finally{
//     console.log("Executed");
// }
// //---------------------------------------------
// class AgeExcep extends Error{
//     constructor(msg){
//         super(msg) ;  
//     }
// }
//    function ageCheck(age)
//     {
//         try {
//             if (age<=18) {
//                 throw new AgeExcep("You are not eligible");
//             } else {
//                 console.log("You are eligible");
//             }
//         } catch (error) {
//             console.log(error.name+"="+error.message);
//         }
//     }
// ageCheck(14)
//---------------------------------------------------------------------------------------
h2
const arr = ["pen", "book", "mobile", "laptop"];
// array destructuring
const [p1, p2,p3,p4] = arr;
console.log(p1+" "+p2); 
console.log(p3+" "+p4); 
console.log("-----Object destructuring-----");
const obj = {
    p_id:12,
    p_name:"Samsung S24",
    p_price:"80000"
}
const{p_id,p_name,p_price}=obj;
console.log(p_id); 
console.log(p_name);
console.log(p_price);
-----------------------------------------------
class Product{
    name="IQO Neo 9";
    productId=21;
    price=38000
    displayInfo(){
        console.log("productid= "+this.productId+"\n"+this.name+"\n"+this.price);
    }
}
const product=new Product;
product.displayInfo();
-----------------------------------------------
const product=
    {pname:"Mobile",price:20000,stock:2}
function showproduct(){
    return new Promise((resolve, reject) => {
        let success=true
         if (success) {
             resolve(product);
         } else {
             reject("unsuccess")         
         }
     });
 }
 showproduct().then((result) => 
     console.log(result)
 ).catch(() => {
     console.log("error");
 });
----------------------------------------------
const ordersHistory=[
    {id:1,pname:"Mobile",price:20000},
    {id:2,pname:"phone",price:10000},
    {id:3,pname:"laptop",price:40000}]
function displayorderHistory(){
    return new Promise((resolve, reject) => {
        let success=true;
        if (success) {
            resolve(ordersHistory);
        } else {
            reject("error")
        }
    });
}
async function fetch_history() {
    let c=await displayorderHistory();
    console.log(c);
}
fetch_history();
-------------------------------------------------
function product(mouse,keyboard,cpu,monitor,discount=20){
    total=mouse+keyboard+cpu+monitor
    console.log("Total = "+total);
    discountprice=total-(total*discount)
console.log("after discout= "+discountprice);
}
product(500,600,20000,5000);
product(500,600,20000,5000,20);
console.log("----------------")
function productPrices(...price){
   return price;
}
console.log( productPrices(100,200,300,400));
------------------------------------------------------
function processPayment(paymentDetails) {
    if (!paymentDetails.cardNumber)
         throw new Error("Payment failed: Card number is missing");
    if (paymentDetails.amount <= 10) 
        throw new Error("Payment failed: Invalid amount");
    return "Payment successful";
}
function checkout() {
    const paymentDetails = {
        cardNumber: "1234-5678-9012-3456",
        amount: 50, 
    };
    try {
        const result = processPayment(paymentDetails);
        console.log(result);
    } catch (error) {
        console.error("Error during payment:", error.message);
        if (error.message.includes("Card number")) {
            console.error("Please check your card details.");
        } else if (error.message.includes("Invalid amount")) {
            console.error("Please check the amount.");
        } else {
            console.error("Network issue, please try again.");
        }
    }
}
checkout();
----------------------------------------------------
function checkInventory(productId) {
    const inventory = {
        "11": 5,   
        "12": 0     
    };
    if (inventory[productId] <= 0) {
        throw new Error("out of stock");
    }
    return true; 
}
function addToCart(productId) {
    checkInventory(productId); 
    console.log("Product added to cart successfully.");
}
function handleAddToCart(productId) {
    try {
        addToCart(productId); 
    } catch (error) {
        console.error("Failed to add product to cart:", error.message);
    }
}
handleAddToCart("11");
handleAddToCart("12"); 
-----------------------------------------------------
function customerdetails(info)
{
if(info.phone.length<10){
    throw new Error("invalid phone number");
}
return "Details Saved";
}
function fillDetails(id,name,phone){
    const info={
        id:id,
        name:name,
        phone:phone
    }
try {
    const result=customerdetails(info);
console.log(result);
} catch (error) {
    console.log("Incomplete or invalid details : "+error.message);
}
finally{
    console.log("Loading...");
}}
fillDetails(12,"dhruvin","123456789");
---------------------------------------------------
class InvalidCouponCode extends Error{
    constructor(msg){
        super(msg) ;  
    }
}
   function applyCode(code)
    {
        try {
            if (code!=123456) {
                throw new InvalidCouponCode("Invalid code");
            } else {
                console.log("Congratulations... you got 20% discount");
            }
        } catch (error) {
            console.log(error.name+"="+error.message);
        }
    }
applyCode(12345)
applyCode(123456)
-------------------------------------------------------
class OutofStock extends Error
{
    constructor(msg)
    {super(msg)}
}
   function CheckStock(unit)
    {
        try {
            if (unit<5) {
                throw new OutofStock("Less than 5 Quantity available");
            } else {
                console.log("In Stock = "+ unit);
            }
        } catch (error) {
            console.log(error.name+"="+error.message);
        }
    }
CheckStock(4)
CheckStock(10)
-----------------------------------------------------
Comments
Post a Comment