By GokiSoft.com| 19:33 23/10/2023|
Học JS

Giải phương trình bậc 2 bằng javascript - giải phương trình bậc hai bằng Javascript BT1606

Cho phương trình bậc 2 như sau

a * x2 + b * x + c = 0

với a, b, c : được nhập thông qua hàm promt() trong javascript

Ví du: a = prompt('Nhập giá trị của a') => hiển thị 1 form cho bạn nhập => giá trị nhập sẽ được gán vào a

tương tự làm với b, c

=> In kết quả của x

Liên kết rút gọn:

https://gokisoft.com/1606

Bình luận

avatar
hohuy1610@gmail.com [community,C2108L]
2021-11-24 16:40:11



https://github.com/HoGiaHuy2003/C2108L-HTML-CSS-JS/tree/master/day-8/bt1606


#index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var a 
        a = prompt(`Enter a `)
        var b
        b = prompt(`Enter b `)
        var c
        c = prompt(`Enter c `)
        var d = (b * b) - (4 * a * c)
        if(a == 0) {
            if(b == 0){
                if (c == 0) {
                    document.write('All are solutions of x')
                  
                }
                else{
                    document.write(`Can't solve for x`)
                }
            }
            else{
                document.write('x = '+(-c/b))
            }
        }
    else if (d >= 0) {
            document.write(`x1 = `+ ((-b + Math.sqrt(d)) / (2 * a)))
            document.write(`<br>`)
            document.write(`x2 = `+ ((-b - Math.sqrt(d)) / (2 * a)))
        } else {
            document.write(`Can't solve for x`)
        }
    </script>
</body>
</html>


avatar
Đào Mạnh Dũng [community,C2010L]
2021-02-23 15:15:15



<!DOCTYPE html>
<html>
<head>
	<title>Phương trình bậc 2</title>
</head>
<body>
<script type="text/javascript">

var a = prompt('Nhập giá trị của a')
var b = prompt('Nhập giá trị của b')
var c = prompt('Nhập giá trị của c')


	 if(a == 0) {
         
        if(b == 0) {
            if (c == 0) {
            	alert('Phuong trinh vo so nghiem')
                 
            } else {
                alert('Phuong trinh vo nghiem')
            }
        } else {
            alert('Phuong trinh co nghiem duy nhat: '+(-c/b))
        }
    } else {
        var delta = b*b - 4*a*c;
        if(delta > 0) {
            var x1 = (-b+Math.sqrt(delta))/(2*a);
            var x2 = (-b-Math.sqrt(delta))/(2*a);
            alert('Nghiem thu nhat x1 = '+x1+'\n Nghiem thu hai x2 = '+x2)
        } else if ( delta == 0) {
        	var sum = -b/2*a
            alert('Phuong trinh co nghiem kep: x1 = x2 = '+sum)
        } else {
            alert('Phuong trinh vo nghiem')
        }
    }
</script>
</body>
</html>


avatar
Vũ Ngọc Văn [community,C2010L]
2021-02-23 14:24:53



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Phương trình bậc 2</title>
    <!-- <script src="js/bai2-phuongtrinhbachai.js"></script> -->
</head>
<body>
<!-- Cho phương trình bậc 2 như sau
a * x2 + b * x + c = 0
với a, b, c : được nhập thông qua hàm promt() trong javascript
Ví du: a = prompt('Nhập giá trị của a') => hiển thị 1 form cho bạn nhập => giá trị nhập sẽ được gán vào a
tương tự làm với b, c
=> In kết quả của x -->

<script>
var a = prompt("Nhập số a:")
var b = prompt("Nhập số b:")
var c = prompt("Nhập số c:")
var delta = b*b - 4*a*c

if (isNaN(a) || isNaN(b) || isNaN(c)){
    alert("Hãy nhập vào số!")
}
else if (delta < 0){
    alert("Phương trình vô nghiệm!")
}
else if (delta == 0){
    x = -b/(2*a)
    alert("Phương trình có nghiệm kép là x1 = " + "x2 = " + x)
}
else if (delta > 0){
    x1 = (-b + Math.sqrt(delta))/(2*a)
    x2 = (-b - Math.sqrt(delta))/(2*a)
    alert("Phương trình có hai nghiệm phân biệt là x1 = " + x1 + " và x2 = " + x2)
}
</script>
</body>
</html>


avatar
Le Duc Viet [community,C2010L]
2021-02-23 13:51:37


#2.html


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
		var a = prompt('a = ')
		var b = prompt('b = ')
		var c = prompt('c = ')
		if (a==0) {
			if (b==0) {
				if (c==0) {
					alert("vo so nghiem")
				} else alert("vo nghiem")
			} else x = -c/b;alert('x ='+ x)
		} else d= b*b -4*a*c
		if (d>0) {
			x1 = (-b + Math.sqrt(d)) / (2*a)
			x1 = (-b - Math.sqrt(d)) / (2*a)
			alert ('pt co 2 nghiem la '+x1 +2)
		} else if (d==0) {
			x = -b/2*a
			alert ('pt co nghiem kep la x1 = x2 =' +x)
		} else alert ("pt vo nghiem")
	</script>
</body>
</html>


avatar
Nguyễn Minh Hiếu [community,C2010L]
2021-02-23 13:29:35



var a = prompt('Nhap a:');
var b = prompt('Nhap b:');
var c = prompt('Nhập c:')
if(a==0){
    x = -c/b;
    console.log('x =' + x);
}
else{
    if(a!=0){
        delta = b*b - 4*a*c;
        if(delta == 0){
            x = -b/(2*a);
            console.log('Phương trình có nghiệm kép là:');
            console.log('x = ' + x);
        }
        else
        {
            x1 =  (-b + Math.sqrt(delta))/(2*a);
            x2 =  (-b - Math.sqrt(delta))/(2*a);
            console.log('Phương trình có hai nghiệm phân biệt là:');
            console.log('x1 = ' + x1);
            console.log('x2 = ' + x2);
        }
    }
}


avatar
Vũ Trung Kiên [C2009I]
2021-02-02 14:16:12


#1606.html


<!DOCTYPE html>
<html>
<head>
	<title>Giải phương trình bậc 2</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
	<style type="text/css">
		*
		{
			padding: 0;
			margin: 0;
			text-align: center;
		}
		.Main
		{
			border: solid 1px #359ee8;
			border-radius: 10px;
			margin-top: 10px;
		}
		.Top
		{
			background-color: #359ee8;
			border-top-left-radius: 8px;
			border-top-right-radius: 8px;
			padding: 5px 0;
			color: white;
		}
		.Bot
		{
			padding: 5px 0;
		}
		.ButtonFUN
		{
			padding: 15px 25px;
  			outline: none !important;
  			color: #fff;
  			background-color: #359ee8;
  			border: none;
  			border-radius: 15px;
  			box-shadow: 0 9px #999;
  			margin: 10px 0;
		}
		.ButtonFUN:hover
		{
			background-color: #2578b3 !important;
		}
		.ButtonFUN:active
		{
			background-color: #359ee8;
  			box-shadow: 0 5px #666;
  			transform: translateY(4px);
		}
		.Button
		{
			margin: 10px 0;
		}
	</style>
</head>
<body>
	<div class="container">
		<div class="Main">
			<div class="Top">
				<h4>
					Giải phương trình bậc 2
				</h4>
			</div>
			<div class="Bot">
				<div class="Text">
					<span id="A">a</span>x<sup>2</sup>+<span id="B">b</span>x+<span id="C">c</span>=0 (a≠0)
					<br>
					<span id="End"></span>
				</div>
				<div class="Button">
					<button class="ButtonFUN" type="button" onclick="CheckA()">Nhập A</button>
					<button class="ButtonFUN" type="button" onclick="EnterB()">Nhập B</button>
					<button class="ButtonFUN" type="button" onclick="EnterC()">Nhập C</button>
					<button class="ButtonFUN" type="button" style="background-color: green" onclick="Done()">Giải phương trình</button>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		function CheckA () {
			do {
				a = prompt('Nhập giá trị a');
			} while (a == 0);
			document.getElementById('A').innerHTML = a;
		}
		function EnterB () {
			b = prompt('Nhập giá trị b');
			document.getElementById('B').innerHTML = b;
		}
		function EnterC () {
			c = prompt('Nhập giá trị b');
			document.getElementById('C').innerHTML = c;
		}
		function Done () {
			delta = b * b - 4 * a * c;
			if (delta < 0) {
				document.getElementById('End').innerHTML = 'Phương trình vô nghiệm';
			} else if (delta == 0) {
				nk = -b / (2 * a);
				document.getElementById('End').innerHTML = 'Phương trình có nghiệm kép:' + '<br>' + ' x<sub>1</sub> = x<sub>2</sub> = ' + nk;
			} else {
				n1 = (-b + Math.sqrt(delta)) / (2 * a);
				n2 = (-b - Math.sqrt(delta)) / (2 * a);
				document.getElementById('End').innerHTML = 'Phương trình có 2 nghiệm phân biệt:' + '<br>' + 'x<sub>1</sub> = ' + n1 + '<br>' + 'x<sub>2</sub> = ' + n2;
			}
		}
	</script>
</body>
</html>


avatar
silentvoice [C2009I]
2020-12-17 07:00:40

https://phamngoclong.herokuapp.com/baitaplab6/bai1/bai1.html


avatar
nguyen hoang viet [community,C2009I]
2020-12-16 17:26:48

https://nguyenhoangviet.herokuapp.com/pt_bac2.html

avatar
Lê Trọng Nghĩa [community,C2009I]
2020-12-16 02:49:29



<!DOCTYPE html>
<html>
<head>
	<title>Giải Phương Trình Bậc 2</title>
	<meta charset="utf-8">
	<style type="text/css">
		body{
			background-color: red;
			
		}
	</style>
</head>
<body>
	<script type="text/javascript">
		do{
			var a = prompt('Nhập giá trị của a')
		}while(a == 0)
		var a = prompt('Nhập giá trị của a')
		
		var b = prompt('Nhập giá trị của b')
		
		var c = prompt('Nhập giá trị của c')
		
		d = b*b-4*a*c
		k = -b / (2 * c)
		d1 = (-b + Math.sqrt(d)) / (2*a)
		d2 = (-b - Math.sqrt(d)) / (2*a)

	if(d < 0)
	{
		document.write('Phương trình vô nghiệm')
	}else if(d == 0)
	{
		document.write('Phương trình có nghiệm kép x1 = x2 ='+k)	
	}else 
	{
		document.write('Phương trình có 2 nghiệm'+'<br>')
		document.write('x1 = ' + d1 +'<br>') 
		document.write('x2 = ' + d2 )
	}
	</script>
</body>
</html>


avatar
Vũ Trung Kiên [C2009I]
2020-12-15 09:24:28


#PTBac2.html


<!DOCTYPE html>
<html>
<head>
	<title>Giải Phương Trình Bậc 2</title>
	<meta charset="utf-8">
</head>
<body>
	<script type="text/javascript">
		do
		{
			a = prompt('Nhap vao a: ');
		} while (a == 0)
		b = prompt('Nhap vao b: ');
		c = prompt('Nhap vao c: ');
		dt = b * b - 4 * a *c;
		k = (-b) / (2*a);
		d1 = (-b + Math.sqrt(dt)) / (2 * a);
		d2 = (-b - Math.sqrt(dt)) / (2 * a);
		if (dt < 0)
		{
			document.write('Phương trình vô nghiệm')
		} else if(dt == 0)
		{
			document.write('Phương trình có nghiệm kép x1 = x2 ='+k)
		} else 
		{
			document.write('Phương trình có 2 nghiệm phân biệt'+'<br>')
			document.write('x1 = ' + d1 +'<br>')
			document.write('x2 = ' + d2)
		}
	</script>
</body>
</html>