Javascript - Cộng hai số nguyên
Cộng hai số nguyên
Viết chương trình JavaScript để tính tổng của hai số nguyên đã cho. Nếu hai giá trị giống nhau, thì trả về tổng của chúng gấp ba lần.
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to compute the sum of the two given integers. If the two values are same, then return triple their sum</title>
<script>
function sumTriple (x, y) {
if (x == y) {
return 3 * (x + y);
}
else
{
return (x + y);
}
}
document.write(sumTriple(10, 20));
document.write(sumTriple(10, 10));
</script>
</head>
<body>
</body>
</html>
Lưu đồ thuật toán: