DEV Community

Karthick (k)
Karthick (k)

Posted on

JavaScript scenario questions (Variables, operators, conditional Statements)

Q3. Store a student’s marks in 3 subjects and calculate the total.

let Tamil=90
let English=89
let maths=55
Total=Tamil+English+maths
console.log(
subjects and calculate total.${Total});

Q4. Swap two numbers without using a third variable.

let A=10
let B=15
A=A+B
B=A-B
A=A-B
console.log(A)
console.log(B)

Q5. Create a variable that stores whether a user is logged in or not, and show a welcome message.

` let user_name=" I am Karthick."
let Login=true

    if (Login)
    {
        console.log(`Welcome ${user_name}`)
    }
    else{
        console.log("Please login")
    }
Enter fullscreen mode Exit fullscreen mode

`
Q6. An e-commerce website gives a 10% discount. Calculate the final price.

` let Actual_price=10000
let discount=20

    let Total_price;

    Total_price=Actual_price

    let price=Total_price-Total_price/discount

    let discount_price=price

    console.log("Final price : "+discount_price)
Enter fullscreen mode Exit fullscreen mode


Q7. Check whether a number is even or odd using an operator.
let Given_number=89

    if (Given_number%2==0){
        console.log('It is Even Number')
    }
    else{
        console.log('It is odd number')
    }`
Enter fullscreen mode Exit fullscreen mode

Q8.Compare two passwords entered by the user.

` let User_1="Karthick_06";
let User_2="KJkjkhgg_08";

    if (User_1 == User_2){
        console.log('it is True')
    }
    else{
        console.log('it is False')
    }
Enter fullscreen mode Exit fullscreen mode

`

Q9.A person is eligible to vote if their age is >= 18. Write a condition.

` let person_age=27

    if (person_age>=18)
    {
        console.log('Eligible for vote')
    }
    else{
        console.log('Not Eligible for vote')
    }
Enter fullscreen mode Exit fullscreen mode


Q10. Use logical AND to check:
User is logged in, User is admin, then allow dashboard access.
let userlogin=true
let user="Admin"

    if (userlogin && user == "Admin"){
        console.log('Welcome to Dashbord')
    }
    else{
        console.log('Access Deneid')
    }
Enter fullscreen mode Exit fullscreen mode

`

Q11.Create a function that calculates the total cart amount.

` function cart_total(){
let item1=500;
let item2=1000;
let item3=300;

        let total=item1+item2+item3
        console.log("Total amount :",total)
    }`
Enter fullscreen mode Exit fullscreen mode

Q12. Create a function to greet a user by name.

function greet(username){
console.log("Welcome"+" "+username+"!")
}
greet("Karthick")

Q13.Create a function to convert Celsius to Fahrenheit.

` function converts(celsius){
return (celsius* 9/5)+32;
}

    let celsius=42;
    let fahrenhint=converts(celsius);
    console.log(fahrenhint)`
Enter fullscreen mode Exit fullscreen mode

Q14.Create a function that returns the largest of two numbers.
function largest(n1,n2){
if (n1>n2){
return n1;
}
else if (n2>n1){
return n2;
}
else{
return ("Both are the same")
}
}

Top comments (0)