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

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2020-10-16 09:19:50



<!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>



Nguyễn Hữu Hiếu [T2008A]
Nguyễn Hữu Hiếu

2020-10-16 08:51:28


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

2020-10-16 08:35:49



<!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>



Le Van Dung [HTML5-T6]
Le Van Dung

2020-06-10 13:46:55


#flynn1006.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function math(a,b){
            document.write("Tổng a và b =",a+b);
            document.write("<br>Hiệu a và b =",a-b);
            document.write("<br>Tích a và b =",a*b);
            document.write("<br>Thương a và b =",a/b);
        }
        var a = parseInt(prompt("Nhập giá trị của a",''));
        var b = parseInt(prompt("Nhập giá trị của b",''));
        math(a,b);
    </script>
</body>
</html>