By GokiSoft.com| 19:19 25/10/2023|
Học JS

Bài tập ôn luyện Function - Lập trình Javascript BT2007

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.

Liên kết rút gọn:

https://gokisoft.com/2007

Bình luận

avatar
hainguyen [T2008A]
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>


avatar
Nguyễn Tiến Đạt [T2008A]
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>


avatar
Triệu Văn Lăng [T2008A]
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>


avatar
Đỗ Minh Quân [T2008A]
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>


avatar
Nguyên Phấn Đông [T2008A]
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>


avatar
Nguyên Phấn Đông [T2008A]
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>


avatar
Nguyễn Hữu Hiếu [T2008A]
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>


avatar
Đặng Trần Nhật Minh [T2008A]
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>