Skip to content

Instantly share code, notes, and snippets.

@eduardolundgren
Created September 25, 2012 20:17
Show Gist options
  • Save eduardolundgren/3784188 to your computer and use it in GitHub Desktop.
Save eduardolundgren/3784188 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<script src="jquery-ui-1.8.23.custom/js/jquery-1.8.0.min.js"></script>
<script src="jquery-ui-1.8.23.custom/js/jquery-ui-1.8.23.custom.min.js"></script>
<script src="jquery.tools.min.js"></script>
<link rel="stylesheet" href="jquery-ui-1.8.23.custom/css/ui-lightness/jquery-ui-1.8.23.custom.css">
<title>Calcule seu IMC</title>
<script type="text/javascript">
$(document).ready(function() {
var INDICES = [
[0, 18.5, "abaixo do peso ideal"],
[18.6, 24.9, "peso normal"],
[25, 29.9, "sobrepeso"],
[30, 34.9, "obesidade 1"],
[35, 39.9, "obesidade 2"],
[40, 100, "obesidade 3"]
];
function Pessoa(nome, idade, peso, altura){
this.nome = nome;
this.idade = idade;
this.peso = peso;
this.altura = altura;
}
function IMC() {
this.listaPessoas = [];
}
IMC.prototype = {
listaPessoas: null,
armazenaDados: function(pessoa) {
var instance = this;
instance.listaPessoas.push(pessoa);
},
calcular: function(pessoa) {
var instance = this;
return pessoa.peso/Math.pow(pessoa.altura/100, 2);
},
listaDados: function() {
var instance = this;
$.each(instance.listaPessoas, function(i, pessoa) {
$('#lista').append(instance.serializar(pessoa));
});
},
resultado: function(valor){
var instance = this,
testeIMC = function(valor) {
var indice, i = 0;
this.valor = valor;
while (indice = INDICES[i++]) {
if ((valor >= indice[0]) && (valor <= indice[1])) {
return indice[2];
}
}
};
return testeIMC(valor);
},
serializar: function(pessoa) {
var instance = this;
var valorIMC = instance.calcular(pessoa).toFixed(2);
var resultadoIMC = instance.resultado(valorIMC);
return [
'<p>',
pessoa.nome,
' (', valorIMC, ') ',
resultadoIMC,
'</p>'
];
}
};
$("button").click(function(){
var imc = new IMC();
var nome = $("#nome").val();
var idade = $("#idade").val();
var peso = $("#peso").val();
var altura = $("#altura").val();
var pessoa = new Pessoa(nome, idade, peso, altura);
$( "#dialog-confirm" ).dialog({
buttons: {
"Sim": function() {
imc.armazenaDados(pessoa);
imc.listaDados();
$("#campos").get(0).reset();
$(this).dialog("close");
},
"Não": function() {
imc.armazenaDados(pessoa);
imc.listaDados();
$(this).dialog("close");
}
},
height: 140,
modal: true,
resizable: false
});
});
$("#altura").change(function(){
$("#range").html($(this).val());
});
});
</script>
</head>
<body>
<form action="" id="campos">
<fieldset>
<label for="nome">Nome:</label>
<input type="text" id="nome">
<br>
<label for="idade">Idade:</label>
<input type="number" id="idade" min="12" max="99" >
<br>
<label for="peso">Peso:</label>
<input type="number" id="peso" min="16" max="200" >
<br>
<label for="altura">Altura:</label>
<input type="range" id="altura" min="20" max="230">
<small id="range"></small>
<br>
<button type="button">Enviar</button>
</fieldset>
</form>
<div id="lista" style="background: pink;">
</div>
<div id="dialog-confirm" title="Cadastro IMC" style="display: none;">
<p>
<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
Deseja cadastrar mais um?
</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment