0% found this document useful (0 votes)
2 views4 pages

Java Script Calculations

The document contains various JavaScript programs that perform different calculations and operations. These include converting kilometers to miles, swapping variables, calculating square roots, checking if a number is positive, negative, or zero, finding the largest among three numbers, sorting words in alphabetical order, and calculating the area of a triangle. Each program takes user input and outputs the result to the console.

Uploaded by

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

Java Script Calculations

The document contains various JavaScript programs that perform different calculations and operations. These include converting kilometers to miles, swapping variables, calculating square roots, checking if a number is positive, negative, or zero, finding the largest among three numbers, sorting words in alphabetical order, and calculating the area of a triangle. Each program takes user input and outputs the result to the console.

Uploaded by

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

Java Script Calculations

// Taking kilometers input from the user


const kilometers = prompt("Enter value in kilometers: ")

// conversion factor
const factor = 0.621371

// calculate miles
const miles = kilometers * factor

[Link](`${kilometers} kilometers is equal to ${miles} miles.`);


-------------------------------------------------------

//JavaScript program to swap two variables

//take input from the users


let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//create a temporary variable


let temp;

//swap variables
temp = a;
a = b;
b = temp;
[Link](`The value of a after swapping: ${a}`);
[Link](`The value of b after swapping: ${b}`);
-------------------------------------------------------

// Take the input from the user and finding square root
const number = prompt('Enter the number: ');
const result = [Link](number);
[Link](`The square root of ${number} is ${result}`);
-------------------------------------------------------

// Take the input from the user and find the square root
const number = prompt('Enter the number: ');

const result = [Link](number);


[Link](`The square root of ${number} is ${result}`);

-------------------------------------------------------

// Program that checks if the number is positive, negative or zero


// input from the user
const number = parseInt(prompt("Enter a number: "));

// check if number is greater than 0


if (number > 0) {
[Link]("The number is positive");
}

// check if number is 0
else if (number == 0) {
[Link]("The number is zero");
}

// if number is less than 0


else {
[Link]("The number is negative");
}

-------------------------------------------------------
// Program to find the largest among three numbers
// take input from the user
const num1 = parseFloat(prompt("Enter first number: "));
const num2 = parseFloat(prompt("Enter second number: "));
const num3 = parseFloat(prompt("Enter third number: "));
let largest;
// check the condition
if(num1 >= num2 && num1 >= num3) {
largest = num1;
}
else if (num2 >= num1 && num2 >= num3) {
largest = num2;
}
else {
largest = num3;
}
// display the result
[Link]("The largest number is " + largest);
-------------------------------------------------------

// Program to sort words in alphabetical order


// take input
const string = prompt('Enter a sentence: ');

// converting to an array
const words = [Link](' ');

// sort the array elements


[Link]();

// display the sorted words


[Link]('The sorted words are:');
for (const element of words) {
[Link](element);
}

// Calculate the area of a triangle

const baseValue = prompt('Enter the base of a triangle: ');


const heightValue = prompt('Enter the height of a triangle: ');

// calculate the area


const areaValue = (baseValue * heightValue) / 2;

[Link](
`The area of the triangle is ${areaValue}`
);

You might also like