0% found this document useful (0 votes)
6 views8 pages

JavaScript Assignment 1

The document contains a series of JavaScript functions that perform various tasks such as calculating areas, converting temperatures, and performing arithmetic operations. Each function prompts the user for input, validates it, and displays the result through alerts. The functions cover a range of topics including geometry, salary calculations, and number guessing.

Uploaded by

Utkarsh Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

JavaScript Assignment 1

The document contains a series of JavaScript functions that perform various tasks such as calculating areas, converting temperatures, and performing arithmetic operations. Each function prompts the user for input, validates it, and displays the result through alerts. The functions cover a range of topics including geometry, salary calculations, and number guessing.

Uploaded by

Utkarsh Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JavaScript Assignment 1

Q1:
Ans: function calculateArea() {

let radius = prompt("Please enter the radius of the circle:")


radius = parseFloat(radius);
if (isNaN(radius) || radius <= 0) {
alert("Please enter a valid positive number for the radius.");
return;
}
const area = [Link] * [Link](radius, 2);
alert("The area of the circle with radius " + radius + " is: " + [Link](2));
}
calculateArea();
Q2:
Ans: function convertFahrenheitToCelsius()
let fahrenheit = prompt("Please enter the temperature in Fahrenheit:");
fahrenheit = parseFloat(fahrenheit);
if (isNaN(fahrenheit)) {
alert("Please enter a valid number for the temperature.");
return;
}
const celsius = (5 / 9) * (fahrenheit - 32
alert("The temperature in Celsius is: " + [Link](2) + "°C");
}
convertFahrenheitToCelsius();
Q3:
Ans: function calculateGrossSalary() {
let basicSalary = prompt("Please enter Ramesh's basic salary”}
basicSalary = parseFloat(basicSalary);
if (isNaN(basicSalary) || basicSalary < 0) {
alert("Please enter a valid positive number for the basic salary.");
return;
}
const dearnessAllowance = 0.40 * basicSalary; // 40% of basic salary
const houseRentAllowance = 0.20 * basicSalary; // 20% of basic salary

const grossSalary = basicSalary + dearnessAllowance + houseRentAllowance;


alert("Ramesh's gross salary is: " + [Link](2));
}
calculateGrossSalary();
Q4:
Ans: function reverseName() {
let firstName = prompt("Please enter your first name:");
let lastName = prompt("Please enter your last name:");
if (!firstName || !lastName) {
alert("Please enter both your first and last name.");
return;
}
let reversedName = lastName + " " + firstName;
alert("Your name in reverse order is: " + reversedName);
}
reverseName();
Q5:
Ans: function calculateVolumeOfSphere(radius) {
const pi = [Link];
const volume = (4 / 3) * pi * [Link](radius, 3);
return volume;
}
const radius = 6;
const volume = calculateVolumeOfSphere(radius);
alert("The volume of the sphere with radius " + radius + " is: " + [Link](2));

Q6:
Ans: function sumOfDigits() {
let number = prompt("Please enter a five-digit number:");
number = parseInt(number, 10);
if (isNaN(number) || number < 10000 || number > 99999) {
alert("Please enter a valid five-digit number.");
return;
}
let sum = 0;
while (number > 0) {
sum += number % 10;
number = [Link](number / 10);
}
alert("The sum of the digits is: " + sum);
}
sumOfDigits();
Q7:
Ans: function calculator() {
let num1 = parseFloat(prompt("Enter the first number:"));
let num2 = parseFloat(prompt("Enter the second number:"));
let operation = prompt("Enter the operation (+, -, *, /):");
let result;
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 === 0) {
alert("Error: Division by zero is not allowed.");
return;
}
result = num1 / num2;
break;
default:
alert("Invalid operation. Please enter +, -, *, or /.");
return;
}
alert("The result of " + num1 + " " + operation + " " + num2 + " is: " + result);
}
calculator();
Q8:
Ans: function calculateSquareRoot() {
let number = parseFloat(prompt("Enter a number to calculate its square root:"));
if (isNaN(number) || number < 0) {
alert("Please enter a valid non-negative number.");
return;
}
let squareRoot = [Link](number);
alert("The square root of " + number + " is: " + squareRoot);
}
calculateSquareRoot();
Q9:
Ans: function calculateTriangleArea()
let a = parseFloat(prompt("Enter the length of side a:"));
let b = parseFloat(prompt("Enter the length of side b:"));
let c = parseFloat(prompt("Enter the length of side c:"));
if (isNaN(a) || isNaN(b) || isNaN(c) || a <= 0 || b <= 0 || c <= 0) {
alert("Please enter valid positive numbers for the sides of the triangle.");
return;
}
if (a + b <= c || a + c <= b || b + c <= a) {
alert("The provided lengths do not form a valid triangle.");
return;
}
let s = (a + b + c) / 2;
let area = [Link](s * (s - a) * (s - b) * (s - c));
alert("The area of the triangle is: " + [Link](2));
}
calculateTriangleArea();
Q10:
Ans: function guessTheNumber() {
const randomNumber = [Link]([Link]() * 10)
let userGuess = parseInt(prompt("Guess a number between 1 and 10:"));
if (isNaN(userGuess) || userGuess < 1 || userGuess > 10) {
alert("Please enter a valid number between 1 and 10.");
return;
}
if (userGuess === randomNumber) {
alert("Good Work! You guessed the correct number: " + randomNumber);
} else {
alert("Not matched. The correct number was: " + randomNumber);
}
}
guessTheNumber();
Q11:
Ans: function checkNumbers(num1, num2) {
return (num1 === 50 || num2 === 50 || (num1 + num2 === 50));
}
let number1 = parseInt(prompt("Enter the first number:"));
let number2 = parseInt(prompt("Enter the second number:"));

if (isNaN(number1) || isNaN(number2)) {
alert("Please enter valid numbers.");
} else {
let result = checkNumbers(number1, number2);
alert("Result: " + result);
}
Q12:
Ans: function findLargerNumber() {
let num1 = parseInt(prompt("Enter the first integer:"));
let num2 = parseInt(prompt("Enter the second integer:"));
if (isNaN(num1) || isNaN(num2)) {
alert("Please enter valid integers.");
return;
}
if (num1 > num2) {
alert("The larger number is: " + num1);
} else if (num2 > num1) {
alert("The larger number is: " + num2);
} else {
alert("Both numbers are equal: " + num1);
}
}
findLargerNumber();
Q13:
Ans: function sortThreeNumbers() {
let num1 = parseFloat(prompt("Enter the first number:"));
let num2 = parseFloat(prompt("Enter the second number:"));
let num3 = parseFloat(prompt("Enter the third number:"));
if (isNaN(num1) || isNaN(num2) || isNaN(num3)) {
alert("Please enter valid numbers.");
return;
}

let sortedNumbers;
if (num1 <= num2 && num1 <= num3) {
if (num2 <= num3) {
sortedNumbers = [num1, num2, num3];
} else {
sortedNumbers = [num1, num3, num2];
}
} else if (num2 <= num1 && num2 <= num3) {
if (num1 <= num3) {
sortedNumbers = [num2, num1, num3];
} else {
sortedNumbers = [num2, num3, num1];
}
} else {
if (num1 <= num2) {
sortedNumbers = [num3, num1, num2];
} else {
sortedNumbers = [num3, num2, num1];
}
}
alert("Sorted numbers: " + [Link](", "));
}
sortThreeNumbers();

Q15:
Ans: function printStarPattern() {
let pattern = "";
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= i; j++) {
pattern += "* ";
}
pattern += "\n";
}
alert(pattern);
}
printStarPattern();

You might also like