Skip to content

Instantly share code, notes, and snippets.

@gabrielleme00
Created May 27, 2019 00:09
Show Gist options
  • Save gabrielleme00/54799a968d8455fdab6b72b00eb2c351 to your computer and use it in GitHub Desktop.
Save gabrielleme00/54799a968d8455fdab6b72b00eb2c351 to your computer and use it in GitHub Desktop.
Fatorial simples em JS/jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ex21</title>
</head>
<body>
<h1>Fatorial</h1>
<div>
<input id="input" type="number">
<button id="btn-calc">Calcular</button>
</div>
<div id="resultado"></div>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
function fatorial(n) {
if (n > 0)
return n * fatorial(n - 1);
else
return 1;
}
$('#btn-calc').click(function() {
let input = parseInt($('#input').val());
if (!isNaN(input))
$('#resultado').text(input + "! = " + fatorial(input));
else
$('#resultado').text("O valor entrado não é um número...");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment