Create a program that uses the methods in the String and Math classes to do the following: ( complete in Javascript)
Hi,
I have added the javascript code below. I you are statisfied with the solution kindly upvote me(I really in need of it). Good day.
Note: To run the code, open chrome browser, right click--> Inspect-->console--> paste this code--> hit enter--> give A,B and C values--> hit enter-->You can see the answer in console
Thanks
===================================code===========================
let rootOne, rootTwo;
// take input from the user
console.log("Hi Please enter the A,B and C value to construct a
quadratic equation like: ax2 + bx + c = 0")
let a = prompt("Enter integer value for A: ");
let b = prompt("Enter integer value for B: ");
let c = prompt("Enter integer value for C: ");
// calculate discriminant
let discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
rootTwo = (-b - Math.sqrt(discriminant)) / (2 * a);
// result
console.log(`The roots of quadratic equation are ${rootOne} and
${rootTwo}`);
}
// condition for real and equal roots
else if (discriminant == 0) {
rootOne = rootTwo = -b / (2 * a);
// result
message = new String(`The roots of quadratic equation are ${rootOne} and ${rootTwo}`);
console.log(message);
}
// if roots are imagenary
else {
let realPartOfEq = (-b / (2 * a)).toFixed(2);
let imagPartOfEq = (Math.sqrt(-discriminant) / (2 *
a)).toFixed(2);
message1 = new String( `The roots of quadratic equation are ${realPartOfEq} + ${imagPartOfEq}i and ${realPartOfEq} - ${imagPartOfEq}i`);
// result
console.log(message1);
}
Get Answers For Free
Most questions answered within 1 hours.