0% found this document useful (0 votes)
4 views

Assignment_3

The document outlines four tasks involving JavaScript functions. Task 1 requires a function to return the maximum, ascending, and descending order of three numbers. Task 2 checks if numbers up to a given limit are odd or even, Task 3 solves a quadratic equation, and Task 4 evaluates a driver's speed against a speed limit with corresponding penalty points.

Uploaded by

divyarawat1623
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment_3

The document outlines four tasks involving JavaScript functions. Task 1 requires a function to return the maximum, ascending, and descending order of three numbers. Task 2 checks if numbers up to a given limit are odd or even, Task 3 solves a quadratic equation, and Task 4 evaluates a driver's speed against a speed limit with corresponding penalty points.

Uploaded by

divyarawat1623
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 3

Task 1

Write a function which returns the maximum, ascending and descending of three number
function processNumbers() {
const a = parseFloat(prompt("Enter the first number:"));
const b = parseFloat(prompt("Enter the second number:"));
const c = parseFloat(prompt("Enter the third number:"));

if (isNaN(a) || isNaN(b) || isNaN(c)) {


console.log("Please enter valid numbers.");
return;
}

const max = Math.max(a, b, c);


const numbers = [a, b, c];
const ascending = [...numbers].sort((x, y) => x - y);
const descending = [...numbers].sort((x, y) => y - x);

console.log(`Maximum: ${max}`);
console.log(`Ascending order: ${ascending.join(", ")}`);
console.log(`Descending order: ${descending.join(", ")}`);
}

processNumbers();
Task 2

Write a function which checks number till given input/parameter is odd or even

function checkOddEven(limit) {
if (isNaN(limit) || limit < 1) {
console.log("Please enter a valid positive number.");
return;
}

for (let i = 1; i <= limit; i++) {


if (i % 2 === 0) {
console.log(`${i} is Even`);
} else {
console.log(`${i} is Odd`);
}
}
}

const limit = parseInt(prompt("Enter a number:"));


checkOddEven(limit);
Task 3

Write a JavaScript program to solve quadratic equation in the form ax2 + bx + c, (Only the

values of a, b and c are provided) the task is to find the roots of the equation.

function solveQuadratic() {
const a = parseFloat(prompt("Enter the value of a:"));
const b = parseFloat(prompt("Enter the value of b:"));
const c = parseFloat(prompt("Enter the value of c:"));

if (isNaN(a) || isNaN(b) || isNaN(c)) {


console.log("Please enter valid numbers for a, b, and c.");
return;
}
if (a === 0) {
console.log("This is not a quadratic equation since a = 0.");
return;
}
const discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
const root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
const root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
console.log(`The roots are real and distinct: ${root1} and ${root2}`);
} else if (discriminant === 0) {
const root = -b / (2 * a);
console.log(`The roots are real and equal: ${root}`);
} else {
const realPart = -b / (2 * a);
const imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
console.log(`The roots are complex: ${realPart} + ${imaginaryPart}i
and ${realPart} - ${imaginaryPart}i`);
}
}
solveQuadratic();
Task 4

Write a function which checks given input/parameter:

• If input/parameter is below speedlimit of 70 print => 'Good Safe Driving'


• If input/parameter is above speedlimit of 70, every 5 kilometers is Penalty Point, print
• 'Speed Limit Crossed by Penalty Point' + Point

If Driver gets more than 10 penalty points ie. above the speed limit 120, print => 'License

Suspended'

function checkSpeed(speed) {
const speedLimit = 70;
const kmPerPoint = 5;

if (speed <= speedLimit) {


console.log('Good Safe Driving');
} else {
const points = Math.floor((speed - speedLimit) / kmPerPoint);

if (points > 10) {


console.log('License Suspended');
} else {
console.log(`Speed Limit Crossed by Penalty Point: ${points}`);
}
}
}

checkSpeed(65);
checkSpeed(80);

You might also like