By GokiSoft.com|
19:31 23/10/2023|
Học JS
Tạo máy tính + - * / trong Javascript - Tạo máy tính căn bản cộng trừ nhân chia bằng Javascript - Sử dụng function trong Javascript
Viết danh sách các hàm sau :
- hàm cộng, trừ, nhân, chia với 2 tham số đầu vào
- Nhập 2 giá trị đầu vào a, b thông qua prompt và biểu thức tính toán +,-,*,/
- Tính kết quả và in ra màn hình.
Chú ý: để nhập a thông qua hàm prompt chúng ta làm theo cách sau
a = prompt('Nhập giá trị a')
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2020-10-16 09:19:50
<!DOCTYPE html>
<html>
<head>
<title>FUNCTION</title>
</head>
<body>
<script type="text/javascript">
var a, b;
a=prompt('nhap gia tri a');
b=prompt('nhap gia tri b');
function tinh_tong(a, b) {
return a+b;
}
var tong=tinh_tong(a, b);
document.write("tong 2 so "+a+"+"+b+ " la "+tong +'<br/>');
function phep_tru(a, b) {
return a-b;
}
var hieu=phep_tru(a, b);
document.write("hieu 2 so "+a+"-"+b+ " la " +hieu +'<br/>');
function phep_nhan(a, b) {
return a*b;
}
var tich=phep_nhan(a, b);
document.write("tich 2 so "+a+"*"+b+ " la " +tich +"<br/>");
function phep_chia(a, b) {
return a/b;
}
var thuong=phep_chia(a, b);
document.write("thuong 2 so "+a+"/"+b+ " la " +thuong +"<br/>");
</script>
</body>
</html>
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2020-10-16 08:51:28
#1622.html
<!DOCTYPE html>
<html>
<head>
<title>Javascript</title>
</head>
<body>
<script type="text/javascript">
function tong(a, b) {
var x = 0;
x = a + b;
return x;
}
function hieu(a, b) {
return a - b;
}
function tich(a, b) {
return a*b;
}
function thuong(a, b) {
return a/b;
}
var a = parseInt(prompt("Nhap a = ", 0));
var b = parseInt(prompt("Nhap b = ", 0));
document.write('Tong = ' + tong(a, b), '<br\>');
document.write('Hieu = ' + hieu(a, b), '<br\>');
document.write('Tich = ' + tich(a, b), '<br\>');
document.write('Thuong = ' + thuong(a, b), '<br\>');
</script>
</body>
</html>
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-10-16 08:35:49
<!DOCTYPE html>
<html>
<head>
<title>CALCULATOR</title>
</head>
<body>
<script type="text/javascript">
var a, b, o;
a = prompt("Nhap a: ")
b = prompt("Nhap b: ")
o = prompt("Nhap phep toan: ")
function sum(a, b) {
return parseInt(a) + parseInt(b);
}
function dif(a, b) {
return a - b;
}
function mul(a, b) {
return a * b;
}
function div(a, b) {
return a / b;
}
if (o === "+") {alert(sum(a, b))}
else if (o === "-") {alert(dif(a, b))}
else if (o === "*") {alert(mul(a, b))}
else if (o === "/") {alert(div(a, b))}
</script>
</body>
</html>
![Le Van Dung [HTML5-T6]](https://www.gravatar.com/avatar/ed8fa837f6d141f47da439b239390e83.jpg?s=80&d=mm&r=g)
Le Van Dung
2020-06-10 13:46:55
#flynn1006.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>
function math(a,b){
document.write("Tổng a và b =",a+b);
document.write("<br>Hiệu a và b =",a-b);
document.write("<br>Tích a và b =",a*b);
document.write("<br>Thương a và b =",a/b);
}
var a = parseInt(prompt("Nhập giá trị của a",''));
var b = parseInt(prompt("Nhập giá trị của b",''));
math(a,b);
</script>
</body>
</html>