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)
![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:09:21
https://github.com/HoGiaHuy2003/C2108L-HTML-CSS-JS/blob/master/day-8/bt1622/index.html
#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>Calculator</title>
</head>
<body>
<h1 style="text-align:center">Calculator</h1>
<script type="text/javascript">
function sum(x,y) {
sum=x+y;
return sum;
}
function sub(x,y) {
sub=x-y;
return sub;
}
function mul(x,y) {
mul=x*y;
return mul;
}
function div(x,y) {
div=x/y;
return div;
}
num1 = parseInt(prompt('Enter number one'))
num2 = parseInt(prompt('Enter number two'))
document.write('Sum of two numbers: ' +sum(num1,num2) +'<br>')
document.write('Difference of two numbers: ' +sub(num1,num2) +'<br>')
document.write('Product of two numbers: ' +mul(num1,num2) +'<br>')
document.write('Quotient of two numbers: ' +div(num1,num2) +'<br>')
</script>
</body>
</html>
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-10-18 13:23:23
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function cong(a,b) {
return a + b;
}
function tru(a,b) {
return a - b;
}
function nhan(a,b) {
return a * b;
}
function chia(a,b) {
return a / b;
}
var a=prompt('Nhap so a')
var b=prompt('Nhap so b')
a=eval(a)
b=eval(b)
document.write('Tổng của a và b là: ' +cong(a,b) + '<br>')
document.write('Hieu cua a va b la:' + tru(a,b) + '<br/>')
document.write('Tich cua a va b la:' + nhan(a,b) + '<br/>')
document.write('Thuong cua a va b la:' + chia(a,b) + '<br/>')
</script>
</body>
</html>
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2020-10-18 10:57:52
#phepTinh.html
<!DOCTYPE html>
<html>
<head>
<title>phepTinh</title>
<meta charset="utf-8">
</head>
<script type="text/javascript">
var a = parseFloat(prompt("Nhap a = "));
var b = parseFloat(prompt("Nhap b = "));
var y =a-b;
var z =a*b;
var k =a/b;
var x =a+b;
document.write("Ket qua phep cong: ", x +"<br/>" );
document.write("Ket qua phep tru: ", y +"<br/>" );
document.write("Ket qua phep nhan: ", z +"<br/>" );
document.write("Ket qua phep chia: ", k +"<br/>" );
</script>
<body>
</body>
</html>
![nguyễn Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
2020-10-16 16:33:43
#máy tính.html
<!DOCTYPE html>
<html>
<head>
<title>may tinh</title>
<script type="text/javascript">
var a = prompt('nhập giá trị a');
var b = prompt('nhập giá trị b');
function cộng(a,b){
return a+b
}
function trừ(a,b){
return a-b
}
function nhân(a,b){
return a*b
}
function chia(a,b){
return a/b
}
document.write('tổng = ' + cộng(a+b)+'<br/>')
document.write('trừ = ' + Trừ(a-b)+'<br/>')
document.write('nhân = ' + nhân(a*b)+'<br/>')
document.write('chia = ' + chia(a/b)+'<br/>')
</script>
</head>
<body>
</body>
</html>
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2020-10-16 16:28:57
#wite.html
<!DOCTYPE html>
<html>
<head>
<title>May Tinh JS</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
function tinh_tong(a, b){
return a + b;
}
function tinh_hieu(a, b){
return a - b;
}
function tinh_tich(a, b){
return a * b;
}
function tinh_thuong(a, b){
return a / b;
}
var a = parseInt(prompt("Nhap a"));
var b = parseInt(prompt("Nhap b"));
var tong = tinh_tong(a, b);
var hieu = tinh_hieu(a, b);
var tich = tinh_tich(a, b);
var thuong = tinh_thuong(a, b);
document.write("Tong 2 so:" + a + "+" + b + "la:" + tong + "</br>");
document.write("Hieu 2 so:" + a + "-" + b + "la:" + hieu + "</br>");
document.write("Tich 2 so:" + a + "*" + b + "la:" + tich + "</br>");
document.write("Thuong 2 so:" + a + "/" + b + "la:" + thuong + "</br>");
</script>
</body>
</html>
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2020-10-16 12:37:47
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript">
var a= prompt ('nhap so a');
var b= prompt ('nhap so b');
function phepcong(a,b){
return a+b
}
function pheptru(a,b){
return a-b
}
function phepnhan(a,b){
return a*b
}
function phepchia(a,b){
return a/b
}
document.write('tong = ' + phepcong(a,b) + '<br/>')
document.write('hiệu = ' + pheptru(a,b) + '<br/>')
document.write('tich = ' + phepnhan(a,b) + '<br/>')
document.write('thương = ' + phepchia(a,b) + '<br/>')
</script>
</head>
<body>
</body>
</html>
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2020-10-16 10:13:29
#tinhtoancongtrunhanchia.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tính toán</title>
</head>
<body style="font-size: 20px;">
<script>
function tong(a,b) {
return a+b
}
function hieu(a,b) {
return a-b
}
function tich(a,b) {
return a*b
}
function thuong(a,b) {
return a/b
}
alert('Bài toán tính toán')
a=prompt('Nhập a:')
b=prompt('Nhập b:')
a=eval(a)
b=eval(b)
document.write('Tổng của a và b là: ' +tong(a,b) + '<br>')
document.write('Hiệu của a và b là: ' +hieu(a,b) + '<br>')
document.write('Tích của a và b là: ' +tich(a,b) + '<br>')
if(b !=0){
document.write('Thương của a và b là: ' +thuong(a,b))
}
else{
alert('Không làm được phép chia!!')
}
</script>
</body>
</html>
![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 10:06:00
#baipheptinh(2007).html
<!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>
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-10-16 09:37:37
#java.html
<!DOCTYPE html>
<html>
<head>
<title>JAVA</title>
</head>
<body>
<script type="text/javascript">
var a, b;
a = prompt('Nhap a')
b = prompt('Nhap b')
function tinhtong(a, b){
return a + b;
}
var tong = tinhtong(a, b)
document.write(a + '+' + b + '=' + tong +'<br/>')
function tinhhieu(a, b){
return a - b;
}
var hieu = tinhhieu(a, b)
document.write( a + '-' + b + '=' + hieu +'<br/>')
function tinhtich(a, b){
return a * b;
}
var tich = tinhtich(a, b)
document.write(a + '*' + b + '=' + tich + '<br/>')
function tinhthuong(a, b){
return a / b;
}
var thuong = tinhthuong(a, b)
document.write( a + '/' + b + '=' + thuong + '<br/>')
</script>
</body>
</html>
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2020-10-16 09:32:21
#Js2.html
<!DOCTYPE html>
<html>
<head>
<title>Js</title>
</head>
<body>
<script type="text/javascript">
var x=prompt("Nhap gia tri x:");
var y=prompt("Nhap gia tri y:");
function cong1(x, y) {
return x + y;
}
cong=cong1(x, y)
alert(cong)
var x=prompt("Nhap gia tri x:");
var y=prompt("Nhap gia tri y:");
function tru1(x, y) {
return x - y;
}
tru=tru1(x, y)
alert(tru)
var x=prompt("Nhap gia tri x:");
var y=prompt("Nhap gia tri y:");
function nhan1(x, y) {
return x * y;
}
nhan=nhan1(x, y)
alert(nhan)
var x=prompt("Nhap gia tri x:");
var y=prompt("Nhap gia tri y:");
function chia1(x, y) {
return x / y;
}
chia=chia1(x, y)
alert(chia)
</script>
</body>
</html>