Hey there, future JavaScript maestros! 🚀 I’m Vishnu Rajoria, your friendly programming trainer at CSLAB Software Development and Training Institute, the best place to kickstart your coding journey in Sikar, Rajasthan.

Today, we’re diving into the fundamentals of JavaScript with a set of 20 assignments that will sharpen your skills in basic math operations. Let’s gear up and make math fun with coding!

🌐 CSLAB: Your Coding Hub in Sikar, Rajasthan 🌐

CSLAB Software Development and Training Institute in Sikar, Rajasthan, is where coding dreams come true. We keep it simple and effective – no jargon, just practical skills. Join us to become the coder you always wanted to be! Visit [cslab.in] and let’s start this coding journey together

🚀 20 JavaScript Math Challenges by Vishnu Rajoria 🚀

Assignment 1: Addition

Write a JavaScript program to add two numbers and display the result.

Assignment 2: Subtraction

Create a script that subtracts one number from another and prints the output.

Assignment 3: Multiplication

Build a program to multiply two numbers and show the result.

Assignment 4: Division

Craft a script to divide two numbers and display the quotient.

Assignment 5: Modulus/Remainder

Write a JavaScript function to find the remainder when one number is divided by another.

Assignment 6: Square

Develop a program that calculates and prints the square of a given number.

Assignment 7: Power of a Number

Create a script to calculate the power of a number raised to a specific exponent.

Assignment 8: Count Digits

Develop a script that counts and displays the number of digits in a given integer.

Assignment 9: Check Positive, Negative, or Zero

Craft a function that checks whether a given number is positive, negative, or zero.

Assignment 10: Swap Numbers

Write a JavaScript program to swap the values of two variables without using a temporary variable.

Assignment 11: Minimum

Write a JavaScript function to find the minimum of two numbers.

Assignment 12: Maximum

Develop a program that determines the maximum of two given numbers.

Assignment 13: Average

Create a script to calculate and display the average of three numbers.

Assignment 14: Check Even or Odd

Build a function to check if a given number is even or odd.

Assignment 15: Sum of Natural Numbers

Write a program to find the sum of the first 10 natural numbers.

Assignment 16: Factorial

Craft a script to calculate the factorial of a given number.

Assignment 17: Calculate Simple Interest

Create a program to calculate and display the simple interest based on user input.

Assignment 18: Prime Number Checker

Create a program that checks whether a given number is prime or not.

Assignment 19: Calculate Grocery Bill

Write a JavaScript program that calculates the total cost of a grocery bill. Include variables for the prices of individual items and quantities purchased.

Assignment 20: Convert Currency

Develop a script that converts an amount from one currency to another. Include variables for exchange rates.

Assignment Solution

Assignment 1: Addition

Write a JavaScript program to add two numbers and display the result.

Solution:

Here’s a simple JavaScript program that adds two numbers and displays the result

// Prompt the user to enter the first number
let firstNumber = parseFloat(prompt("Enter the first number:"));

// Prompt the user to enter the second number
let secondNumber = parseFloat(prompt("Enter the second number:"));

// Check if the inputs are valid numbers
if (isNaN(firstNumber) || isNaN(secondNumber)) {
    alert("Please enter valid numbers.");
} else {
    // Calculate the sum of the two numbers
    let sum = firstNumber + secondNumber;

    // Display the result
    alert(`The sum of ${firstNumber} and ${secondNumber} is: ${sum}`);
}
  • This program uses the prompt function to get user input
  • converts the input to numbers using parseFloat
  • checks if the inputs are valid numbers using isNaN
  • performs the addition, and then displays the result using alert.

Assignment 2: Subtraction

Create a script that subtracts one number from another and prints the output.

Solution

Here’s a simple JavaScript program that subtracts one number from another and prints the output:

// Prompt the user to enter the first number
let firstNumber = parseFloat(prompt("Enter the first number:"));

// Prompt the user to enter the second number
let secondNumber = parseFloat(prompt("Enter the second number:"));

// Check if the inputs are valid numbers
if (isNaN(firstNumber) || isNaN(secondNumber)) {
    alert("Please enter valid numbers.");
} else {
    // Calculate the difference between the two numbers
    let difference = firstNumber - secondNumber;

    // Display the result
    alert(`The difference between ${firstNumber} and ${secondNumber} is: ${difference}`);
}

This program is similar to the previous one but performs subtraction instead of addition. It prompts the user to enter two numbers, checks if the inputs are valid, calculates the difference, and then displays the result using alert.

Assignment 3: Multiplication

Build a program to multiply two numbers and show the result.

Solution

Here’s a simple JavaScript program that multiplies two numbers and displays the result:

// Prompt the user to enter the first number
let firstNumber = parseFloat(prompt("Enter the first number:"));

// Prompt the user to enter the second number
let secondNumber = parseFloat(prompt("Enter the second number:"));

// Check if the inputs are valid numbers
if (isNaN(firstNumber) || isNaN(secondNumber)) {
    alert("Please enter valid numbers.");
} else {
    // Calculate the product of the two numbers
    let product = firstNumber * secondNumber;

    // Display the result
    alert(`The product of ${firstNumber} and ${secondNumber} is: ${product}`);
}

Assignment 4: Division

Craft a script to divide two numbers and display the quotient.

Solution:

Here’s a simple JavaScript program that divides two numbers and displays the quotient:

// Prompt the user to enter the dividend
let dividend = parseFloat(prompt("Enter the dividend:"));

// Prompt the user to enter the divisor
let divisor = parseFloat(prompt("Enter the divisor:"));

// Check if the inputs are valid numbers and if the divisor is not zero
if (isNaN(dividend) || isNaN(divisor) || divisor === 0) {
    alert("Please enter valid numbers. The divisor cannot be zero.");
} else {
    // Calculate the quotient of the two numbers
    let quotient = dividend / divisor;

    // Display the result
    alert(`The quotient of ${dividend} divided by ${divisor} is: ${quotient}`);
}

more solutions coming soon! till then try the assignment.

🌟 Ready to Code at CSLAB? Join Us Today! 🌟

Remember, practice is the key to mastering JavaScript and turning these mathematical operations into second nature. Happy coding, and see you in class at CSLAB Software Development and Training Institute! 🚀💻

Happy Coding, Future Developers! 🚀💻


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *