By GokiSoft.com|
19:19 25/10/2023|
Học JS
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)
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-10-16 13:58:53
#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>
![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:14:01
#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:05:31
#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>
![Đỗ Minh Quân [T2008A]](https://www.gravatar.com/avatar/fa40264d7c4b4209c87a9e9451d2b9f0.jpg?s=80&d=mm&r=g)
Đỗ Minh Quân
2020-10-16 09:19:45
#bai2.html
<!DOCTYPE html>
<html>
<head>
<title>bai 2</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
function tinhtong(a,b){
return a+b;
}
function tinhhieu(a,b){
return a-b;
}
function tinhnhan(a,b){
return a*b;
}
function tinhchia(a,b){
return a/b;
}
alert("tinh toan phan 1")
x=prompt("nhap vao so x:")
y=prompt("nhap vao so y:")
document.write("tong cua x va y la:"+ tinhtong(x,y) + "<br>")
document.write("hieu cua x va y la:"+ tinhhieu(x,y) + "<br>")
document.write("nhan cua x va y la:"+ tinhnhan(x,y) + "<br>")
document.write("chia cua x va y la:"+ tinhchia(x,y) + "<br>")
</script>
</body>
</html>
![Nguyên Phấn Đông [T2008A]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2020-10-16 09:03:01
<!DOCTYPE html>
<html>
<head>
<title>bieu thuc</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
function tinh_tong(a, b)
{
return a + b;
}
var a = parseInt(prompt("Nhập số a"));
var b = parseInt(prompt("Nhập số b"));
var tong = tinh_tong(a, b);
document.write("Tổng hai số " + a + " + " + b + " là " + tong+'<br/>');
function tinh_hieu(a, b)
{
return a - b;
}
var hieu = tinh_hieu(a, b);
document.write("Hiệu hai số " + a + " - " + b + " là " + hieu+'<br/>');
function tinh_chia(a, b)
{
return a / b;
}
var chia = tinh_chia(a, b);
document.write("Chia hai số " + a + " / " + b + " là " + chia);
</script>
</body>
</html>
![Nguyên Phấn Đông [T2008A]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2020-10-16 09:03:00
<!DOCTYPE html>
<html>
<head>
<title>bieu thuc</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
function tinh_tong(a, b)
{
return a + b;
}
var a = parseInt(prompt("Nhập số a"));
var b = parseInt(prompt("Nhập số b"));
var tong = tinh_tong(a, b);
document.write("Tổng hai số " + a + " + " + b + " là " + tong+'<br/>');
function tinh_hieu(a, b)
{
return a - b;
}
var hieu = tinh_hieu(a, b);
document.write("Hiệu hai số " + a + " - " + b + " là " + hieu+'<br/>');
function tinh_chia(a, b)
{
return a / b;
}
var chia = tinh_chia(a, b);
document.write("Chia hai số " + a + " / " + b + " là " + chia);
</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 09:02:14
#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:36:17
<!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>