Bài tập ôn luyện Function - Lập trình 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.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Vũ Trung Kiên [C2009I]](https://www.gravatar.com/avatar/abf994de48c0204239812f8d9a7dcf8c.jpg?s=80&d=mm&r=g)
Vũ Trung Kiên
2020-12-17 15:00:35
#Function.html
<!DOCTYPE html>
<html>
<head>
<title>Function</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
a = prompt('Nhập a:');
b = prompt('Nhập b:');
Tong = parseInt(a) + parseInt(b);
Tich = a * b;
Hieu = a - b;
Thuong = a / b;
document.write('Tổng = ' + Tong + '<br>' + 'Hiệu = ' + Hieu + '<br>' + 'Tích = ' + Tich + '<br>' + 'Thương = ' + Thuong);
</script>
</body>
</html>
![nguyen hoang viet [community,C2009I]](https://www.gravatar.com/avatar/d2766fccea69cebc93358554d1a18e65.jpg?s=80&d=mm&r=g)
nguyen hoang viet
2020-12-17 09:02:40
<!DOCTYPE html>
<html>
<head>
<title>Funtion</title>
</head>
<body>
<script type="text/javascript">
a = prompt('Moi nhap a' ,0)
b = prompt('Moi nhap b' ,0)
c = parseInt(a) + parseInt(b)
d = a - b
e = a * b
f = a / b
document.write('a + b = ' + c + '<br>a - b = ' + d + '<br>a * b = ' + e + '<br>a / b = ' + f)
</script>
</body>
</html>
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2020-10-24 06:57:02
#maytinhjs.html
<!DOCTYPE html>
<html>
<head>
<title>May tinh js</title>
</head>
<body>
<script type="text/javascript">
var a=prompt("Nhap so a")
var b=prompt("Nhap so b")
a=eval(a)
b=eval(b)
function phepcong(a,b) {
return a+b;
}
document.write('Tổng của a và b là: ' +cong(a,b) + '<br>')
function pheptru(a,b) {
return a-b;
}
document.write('Hiệu của a và b là: ' +Tru(a,b) + '<br>')
function phepnhan(a,b) {
return a*b;
}
document.write('Tích của a và b là: ' +nhan(a,b) + '<br>')
function phepchia(a,b) {
return a/b;
}
document.write('Tổng của a và b là: ' +chia(a,b) + '<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:22:29
<!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 11:25:02
#function.html
<!DOCTYPE html>
<html>
<head>
<title>function</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
var a = parseFloat(prompt("Nhap a = "));
var b = parseFloat(prompt("Nhap 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('hieu =' +phepTru(a,b), "<br/>");
document.write('nhan =' +phepNhan(a,b), "<br/>");
document.write('chia =' +phepChia(a,b));
</script>
</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-17 14:41:50
#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>
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2020-10-17 09:15:38
#js5.html
<!DOCTYPE html>
<html>
<head>
<title>Js</title>
</head>
<body>
<script type="text/javascript">
var a,b;
a=prompt("Nhap a")
b=prompt("Nhap b")
function phepcong(a,b) {
return a+b;
}
var tong=phepcong(a,b)
document.write("Tong"+a+"+"+b+ "la:"+tong+"<br/>");
function pheptru(a,b) {
return a-b;
}
var hieu=pheptru(a,b)
document.write("Hieu" +a+"-"+b+ "la:"+hieu+"<br/>");
function phepnhan(a,b) {
return a*b;
}
var tich=phepnhan(a,b)
document.write("Tich" +a+"*"+b+ "la:"+tich+"<br/>");
function phepchia(a,b) {
return a/b;
}
var thuong=phepchia(a,b)
document.write("Thuong" +a+"/" +b+ "la"+thuong+"<br/>");
</script>
</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:45:57
#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>
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2020-10-16 15:37:23
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<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>
</body>
</html>
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
2020-10-16 15:20:00
<!DOCTYPE html>
<html>
<head>
<title>calculator</title>
</head>
<body>
<script type="text/javascript">
a = prompt("Nhap gia tri cua a")
pheptinh = prompt("Nhap phep tinh + - * /")
b = prompt("Nhap gia tri cua b")
function phepcong(a,b){
return parseInt(a) + parseInt(b);
}
function pheptru(a,b){
return a-b;
}
function phepnhan(a,b){
return a*b;
}
function phepchia(a,b){
return a/b;
}
document.write('Ket qua cua phep tinh '+a+pheptinh+b+' =')
if(pheptinh==='+'){
document.write(phepcong(a,b))
} else if (pheptinh==='-'){
document.write(pheptru(a,b))
} else if (pheptinh==='*'){
document.write(phepnhan(a,b))
} else {
document.write(phepchia(a,b))
}
</script>
</body>
</html>