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
Võ Như Việt [C2010L]
2021-02-26 15:10:38



<!DOCTYPE html>
<html>
<head>
	<title>Bài tập ôn luyện Function - Lập trình Javascript</title>
	<meta charset="utf-8">
</head>
<body>
	<h2>Ôn luyện Function</h2>
	<script type="text/javascript">
		var a,b
		function nhapa(){
			a = parseInt(prompt('Nhập giá trị a:'))	}
		function nhapb(){	
			b = parseInt(prompt('Nhập giá trị b:')) }
		function phepcong() {
			document.write('KetQua: ' + (a + b))
		}
		function pheptru() {
			document.write('KetQua: ' + (a - b))
		}
		function phepnhan() {;
			document.write('KetQua: ' + (a * b))
		}
		function phepchia() {
			document.write('KetQua: ' + (a / b))
		}
	</script>
	<div style="padding: 5px">
		<button onclick="nhapa()">Nhập giá trị A</button><br>
		<button onclick="nhapb()">Nhập giá trị B</button><br>
	</div>
	<div style="padding: 5px">
		<button style="line-height: 1.5" onclick="phepcong()">Kết quả Cộng</button><br>
		<button style="line-height: 1.5" onclick="pheptru()">Kết quả Trừ</button><br>
		<button style="line-height: 1.5" onclick="phepnhan()">Kết quả Nhân</button><br>
		<button style="line-height: 1.5" onclick="phepchia()">Kết quả Chia</button><br>
	</div>
</body>
</html>


avatar
hieuvm0512 [community,C2010L]
2021-02-26 11:50:40



<!DOCTYPE html>
<html>
<head>
	<title>Function</title>
	<meta charset="utf-8">
</head>
<body>
	<h1>FUNCTION</h1>
	<script type="text/javascript">
		var a= parseInt(prompt('Nhap gia tri cua a'))
		console.log(a)
		var b= parseInt(prompt('Nhap gia tri cua b'))
		console.log(b)	

		function cong(x,y)
		{
			return x+y

		}
		function tru(x,y)
		{
			return x-y

		}function nhan(x,y)
		{
            return x*y

		}function chia(x,y)
		{
			return x/y

		}
		var to = cong(a,b)
		var tr = tru(a,b)
		var nh = nhan(a,b)
		var ch = chia(a,b)
			document.write('Tong bang'+to)
			document.write('<br/>')
			document.write('Hieu bang'+tr)
						document.write('<br/>')
			document.write('Tich bang'+nh)
						document.write('<br/>')
			document.write('Thuong bang'+ch)
	</script>


</body>
</html>


avatar
Nguyễn Hùng Phương [community,C2010L]
2021-02-25 16:46:22



<!DOCTYPE html>
<html>
<head>
	<title>Tonghieutichthuong</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('Nhập số A')
		var b=prompt('Nhập số B')
		a=eval(a)
		b=eval(b)
		document.write('Tổng A và B: ' +cong(a,b) + '<br>')
		document.write('Hieu A va B:' + tru(a,b) + '<br/>')
		document.write('Tich A và B:' + nhan(a,b) + '<br/>')
		document.write('Thương A và B:' + chia(a,b) + '<br/>')

	</script>
</body>
</html>


avatar
Bui Duy [community,C2010L]
2021-02-25 16:15:37



<!DOCTYPE html>
<html>
<head>
	<title>onluyenfunction</title>
	<meta charset="utf-8">
</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('Tong 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
Nguyễn Trần Tuân [community,C2010L]
2021-02-25 13:49:15


#25.2.2021bai2.html


<!DOCTYPE html>
<html>
<head>
	<title>Tính giá trị biểu thức</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<div class="main">
		<div class="tittle">
			<h3>Tính giá trị biểu thức</h3>
		</div>
		<div>
			<button type="button" onclick="giatria()">Nhập Giá Trị A</button>
			<button type="button" onclick="giatrib()">Nhập Giá Trị B</button>
			<button type="button" onclick="pheptoan()">Nhập Phép Toán</button>
			<button type="button" onclick="gpt()">In Kết Quả</button>
		</div>
	</div>
	<script type="text/javascript">
		var a,b,c
		function giatria(){
			do{
				a = parseInt(prompt("Nhập giá trị a: "))
			}while(a==0)
		}
		
		function giatrib(){
			b = parseInt(prompt("Nhập giá trị b: "))
		}
		function pheptoan(){
			c = prompt("Nhập phép toán cần thực hiện")
		}
		function gpt(){
			if(c == "+") document.write("Kết quả: " + (a+b))
			if(c == "-") document.write("Kết quả: " + (a-b))
			if(c == "*") document.write("Kết quả: " + (a*b))
			if(c == "/") document.write("Kết quả: " + (a/b))
		}
	</script>
</body>
</html>


avatar
Vũ Ngọc Văn [community,C2010L]
2021-02-25 13:27:10



<!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">
    <!-- <link rel="stylesheet" href="css/bai2_function.css"> -->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .container{
            width: max-content;
            margin: auto;
            margin-top: 200px;
            padding: 12px;
            border: 1px solid black;
        }
    </style>
    <title>Document</title>
</head>
<body>
    <div class="container">



        <!-- <script src="js/bai2_function.js"></script> -->
        <script>
            var a, b, s, h, m, d;

            function nhap_a(){
                a = prompt("Nhập số a:")
                document.writeln("Số a nhập vào là: " + a)
                document.writeln("<br/>")
            }
            function nhap_b(){
                b = prompt("Nhập số b:")
                document.writeln("Số b nhập vào là: " + b)
                document.writeln("<br/>")

            }
            function cong(){
                s = parseInt(a) + parseInt(b);
            }
            function tru(){
                h = parseInt(a) - parseInt(b);
            }
            function nhan(){
                m = parseInt(a)*parseInt(b);
            }
            function chia(){
                d = parseInt(a)/parseInt(b);
            }
            function inketqua(){
                document.writeln(a + " + " + b + " = " + s)
                document.writeln("<br/>")
                document.writeln(a + " - " + b + " = " + h)
                document.writeln("<br/>")
                document.writeln(a + " x " + b + " = " + m)
                document.writeln("<br/>")
                document.writeln(a + " : " + b + " = " + d)
                document.writeln("<br/>")
            }
            nhap_a()
            nhap_b()
            cong()
            tru()
            nhan()
            chia()
            inketqua()
        </script>
    </div>
</body>
</html>


avatar
Nguyễn Minh Hiếu [community,C2010L]
2021-02-25 13:23:14



var a = parseInt(prompt("Nhập a:"))
var b = parseInt(prompt("Nhập b:"))
function Cong(){
    s = a+b;
    document.writeln("Tổng s là " + s)
}
Cong(a,b)
document.writeln("<br/>")

function Tru(){
    s = a-b;
    document.writeln("Hiệu s là " + s)
}
Tru(a,b)
document.writeln("<br/>")

function Nhan(){
    s = a*b;
    document.writeln("Tích s là " + s)
}
Nhan(a,b)
document.writeln("<br/>")

function Chia(){
    s = a/b;
    document.writeln("Thương s là " + s)
}
Chia(a,b)


avatar
Trần Đại Dương [C2010L]
2021-02-25 12:43:51


#Function.html


<!DOCTYPE html>
<html>
<head>
	<title>Function</title>
	<meta charset="utf-8">
	<style type="text/css">
		.math {
			margin: auto;
			width: 30%;
			border: 2px solid #0f99bf;
		}
		.title{
			text-align: : left;
			text-indent: 15px;
			font-weight: bold;
			font-size: 20px;
			border-bottom: 2px solid #0f99bf;
			padding: 3px;
			background-color: #0f99bf;
			color: white;

		}
		.cal{
			margin-left: 15px;
			padding: 3px;
		}
		.cal .buttonCal{
			color: white;
			border-radius: 5px;
			background-color: #54a832;
		}
	</style>
	<script type="text/javascript">
		var a,b,x;
		function nhapa(){
			a = parseInt(prompt("Nhap a: "));
		}
		function nhapb(){
			b = parseInt(prompt("Nhap b: "));
		}
		function cong(){
			x = a + b;	
			alert("Tong A + B = " + x);	
		}function tru(){
			x = a - b;	
			alert("Hieu A - B = " + x);	
		}
		function nhan(){
			x = a * b;	
			alert("Tich A * B = " + x);	
		}
		function chia(){
			x = a / b;	
			alert("Thuong A / B = " + x);	
		}
	</script>

</head>
<body>
	<div class="math">
		<div class="title">
			<span>Math</span>
		</div>
		<div class="cal">
			<button type="button" class="buttonCal" onclick="nhapa()">Nhập giá trị A</button>
			<button type="button" class="buttonCal" onclick="nhapb()">Nhập giá trị B</button>
			<button type="button" class="buttonCal" onclick="cong()">Cộng</button>
			<button type="button" class="buttonCal" onclick="tru()">Trừ</button>
			<button type="button" class="buttonCal" onclick="nhan()">Nhân</button>
			<button type="button" class="buttonCal" onclick="chia()">Chia</button>
		</div>
	</div>	
</body>
</html>


avatar
Vũ Trung Kiên [C2009I]
2021-02-05 13:01:40


#2007.html


<!DOCTYPE html>
<html>
<head>
	<title>Tính toán</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
	<style type="text/css">
		*
		{
			padding: 0;
			margin: 0;
			text-align: center;
		}
		.Main
		{
			border: solid 1px #359ee8;
			border-radius: 10px;
			margin-top: 10px;
		}
		.Top
		{
			background-color: #359ee8;
			border-top-left-radius: 8px;
			border-top-right-radius: 8px;
			padding: 5px 0;
			color: white;
		}
		.Bot
		{
			padding: 5px 0;
		}
		.ButtonFUN
		{
			width: 110px;
			padding: 15px 25px;
  			outline: none !important;
  			color: #fff;
  			background-color: #359ee8;
  			border: none;
  			border-radius: 15px;
  			box-shadow: 0 9px #999;
  			margin: 10px 0;
		}
		.ButtonFUN:hover
		{
			background-color: #2578b3 !important;
		}
		.ButtonFUN:active
		{
			background-color: #359ee8;
  			box-shadow: 0 5px #666;
  			transform: translateY(4px);
		}
		.Button
		{
			margin: 10px 0;
		}
		input
		{
			width: 50px;
		}
		.Text
		{
			margin-top: 10px;
		}
	</style>
</head>
<body>
	<div class="container">
		<div class="Main">
			<div class="Top">
				<h4>
					Tính toán
				</h4>
			</div>
			<div class="Bot">
				<div class="Text">
					<input type="number" id="A" disabled="" required="">
					<input type="text" id="math" disabled="" required="">
					<input type="number" id="B" disabled="" required="">
					= <span id="End">?</span>
				</div>
				<form name="Caculator">
					<input type="textfield" name="Check" hidden="">
				</form>
				<div class="Button">
					<button class="ButtonFUN" type="button" onclick="EnterA()">Nhập a</button>
					<button class="ButtonFUN" type="button" onclick="EnterB()">Nhập b</button>
					<br>
					<button class="ButtonFUN" type="button" onclick="Cong()">Cộng</button>
					<button class="ButtonFUN" type="button" onclick="Tru()">Trừ</button>
					<button class="ButtonFUN" type="button" onclick="Nhan()">Nhân</button>
					<button class="ButtonFUN" type="button" onclick="Chia()">Chia</button>
					<br>
					<button class="ButtonFUN" type="button" onclick="Done()" style="background-color: green">Tính</button>
					<button class="ButtonFUN" type="button" onclick="Reset()" style="background-color: red">Reset</button>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		function EnterA () {
			document.getElementById('A').value = prompt('Nhập giá trị a:');
		}
		function EnterB () {
			document.getElementById('B').value = prompt('Nhập giá trị b:');
		}
		function Cong () {
			document.getElementById('math').value = '+';
		}
		function Tru () {
			document.getElementById('math').value = '-';
		}
		function Nhan () {
			document.getElementById('math').value = '*';
		}
		function Chia () {
			document.getElementById('math').value = '/';
		}
		function Done () {
			document.Caculator.Check.value += document.getElementById('A').value;
			document.Caculator.Check.value += document.getElementById('math').value;
			document.Caculator.Check.value += document.getElementById('B').value;
			document.getElementById('End').innerHTML = eval(document.Caculator.Check.value);
			document.Caculator.Check.value = '';
		}
		function Reset () {
			document.Caculator.Check.value = '';
			document.getElementById('End').innerHTML = '';
		}
	</script>
</body>
</html>


avatar
Lê Trọng Nghĩa [community,C2009I]
2020-12-19 03:05:41



<!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:');
		Cong = parseInt(a) + parseInt(b);
		Nhan = a * b;
		Tru = a - b;
		Chia = a / b;
		document.write('Cộng = ' + Cong + '<br>' + 'Trừ = ' + Tru + '<br>' + 'Nhân = ' + Nhan + '<br>' + 'Chia = ' + Chia);
	</script>
</body>
</html>