By GokiSoft.com|
19:32 23/10/2023|
Học JS
Giải phương trình bậc nhất bằng Javascript
Cho phương trình bậc nhất như sau
a*x + b= 0
Với a, b là số nguyên => tạo ngẫu nhiên
Hiển thị kết quả của x ra màn hình
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![hohuy1610@gmail.com [community,C2108L]](https://www.gravatar.com/avatar/15e0636181e201af8d78b5d3e072fd0b.jpg?s=80&d=mm&r=g)
hohuy1610@gmail.com
2021-11-24 16:38:20
https://github.com/HoGiaHuy2003/C2108L-HTML-CSS-JS/tree/master/day-8/bt1605
#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 `)
if(a == 0) {
if(b == 0){
document.write(`All are solution for x`)
}
else{
document.write(`Can't solve for x`)
}
}
else{
document.write(`x = ` + (-b/a) )
}
</script>
</body>
</html>
![Đào Mạnh Dũng [community,C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
2021-02-23 15:38:24
<!DOCTYPE html>
<html>
<head>
<title>Giải phương trình bậc nhất</title>
</head>
<body>
<script type="text/javascript">
var a = Math.random()*300 -150;
var b = Math.random()*300 -150;
a=parseInt(a);
b=parseInt(b);
alert('ta có với a = '+ a +' và b = '+ b +'\nta có kết quả quả phương trình ax+b=0')
if (a == 0) {
if (b == 0)
{
alert('Phương trình có vô số nghiệm')
}
else
{
alert('Phương trình có vô nghiệm')
}
}
else
alert('Phương trình có một nghiệm là x: ' + (-b/a) )
</script>
</body>
</html>
![Vũ Ngọc Văn [community,C2010L]](https://www.gravatar.com/avatar/3bec4690245af20ea34f68305e3e24b1.jpg?s=80&d=mm&r=g)
Vũ Ngọc Văn
2021-02-23 14:24:00
<!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 1</title>
<!-- <script src="js/bai1-phuontrinhbacnhat.js" type="text/javascript"></script> -->
</head>
<body>
<!--
Cho phương trình bậc nhất như sau
a*x + b= 0
Với a, b là số nguyên => tạo ngẫu nhiên
Hiển thị kết quả của x ra màn hình
-->
<script>
var a = prompt("Nhập giá trị a:")
console.log("a = " + a)
var b = prompt("Nhập giá trị b:")
console.log("b = " + b)
if (isNaN(a) || isNaN(b)){
alert("Hãy nhập vào số!")
}
else if (a == 0){
alert("Phương trình vô nghiệm!")
}
else if (b == 0) {
alert("Phương trình có vô số nghiệm!")
}
else{
x = -b/a
console.log(x)
alert("Kết quả x là: " + x)
}
</script>
</body>
</html>
![Trần Việt Đức Anh [C2010L]](https://www.gravatar.com/avatar/caee507e11365ca2cf5068cbea5c740a.jpg?s=80&d=mm&r=g)
Trần Việt Đức Anh
2021-02-23 13:58:11
<html>
<head>
<link rel="stylesheet" type="Math.css" href="">
<title>First Degree Equation</title>
<script language="JavaScript">
function solve(form){
var x;
var a,b;
a = parseInt(form.a.value);
b = parseInt(form.b.value);
if(a == 0){
if(b == 0) alert("The equation has countless solutions");
else alert("The equation has countless solutions");
}
else{
alert("The equation has one solution");
x=-b/a;
form.x.value = eval(x);
}
}
</script>
</head>
<body>
<form method="post">
<center>
<br> Enter a:
<input type="text" name="a" value="">
<br> Enter b:
<input type="text" name="b" value="">
<br>
<input type="button" value="Solve" onClick="solve(this.form)">
<br>
</center>
</form>
</body>
</html>
![Le Duc Viet [community,C2010L]](https://www.gravatar.com/avatar/3b533532c7ace3144eccd4cbb3436d9b.jpg?s=80&d=mm&r=g)
Le Duc Viet
2021-02-23 13:35:08
#1.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 = ')
if (a==0) {
if (b==0){
alert('vo so nghiem')
}
if (b!=0){
alert('vo nghiem')
}
}else
var x = -b/a
alert('x = ' + x)
</script>
</body>
</html>
![Nguyễn Minh Hiếu [community,C2010L]](https://www.gravatar.com/avatar/cd93ed3dad5ea834fc22cba6a363c9d9.jpg?s=80&d=mm&r=g)
Nguyễn Minh Hiếu
2021-02-23 13:16:20
var a = prompt('Nhap a:');
var b = prompt('Nhap b:');
if(a==0){
console.log('phương trình vô nghiệm');
}
else{
c = -b/a;
console.log(c);
}
![Vũ Trung Kiên [C2009I]](https://www.gravatar.com/avatar/abf994de48c0204239812f8d9a7dcf8c.jpg?s=80&d=mm&r=g)
Vũ Trung Kiên
2021-02-02 14:26:44
#1605.html
<!DOCTYPE html>
<html>
<head>
<title>Giải phương trình bậc 1</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 1
</h4>
</div>
<div class="Bot">
<div class="Text">
<span id="A">a</span>x+<span id="B">b</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" 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 Done () {
kq = -b / a;
document.getElementById('End').innerHTML = 'Phương trình có nghiệm:' + '<br>' + 'x = ' + kq;
}
</script>
</body>
</html>
![silentvoice [C2009I]](https://www.gravatar.com/avatar/9e5d35bff7e4f5191ba44a5e273bd9ca.jpg?s=80&d=mm&r=g)
silentvoice
2020-12-17 07:01:29
https://phamngoclong.herokuapp.com/baitaplab6/bai2/bai2.html
![nguyen hoang viet [community,C2009I]](https://www.gravatar.com/avatar/d2766fccea69cebc93358554d1a18e65.jpg?s=80&d=mm&r=g)
nguyen hoang viet
2020-12-16 17:26:13
https://nguyenhoangviet.herokuapp.com/pt_bac1.html
![Pham Tuan Anh [community,C2009I]](https://www.gravatar.com/avatar/9f51282c60970134131773debb671510.jpg?s=80&d=mm&r=g)
Pham Tuan Anh
2020-12-16 16:38:42
#phuongtrinhbac1.html
<!DOCTYPE html>
<html>
<head>
<title>Phuong trinh bac nhat</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
var a = prompt('Nhap gia tri a',0),
b = prompt('Nhap gia tri b',0),
x=(-b)/a
alert("x="+x)
console.log(x)
</script>
</body>
</html>