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 BT1622

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')

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

https://gokisoft.com/1622

Bình luận

avatar
hohuy1610@gmail.com [community,C2108L]
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>


avatar
Do Trung Duc [T2008A]
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>


avatar
Trần Thị Khánh Huyền [T2008A]
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>


avatar
nguyễn Sử [T2008A]
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>


avatar
Trần Văn Lâm [T2008A]
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>


avatar
vuong huu phu [T2008A]
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>


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


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


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


avatar
Đức Sơn [T2008A]
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>