Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
box-calculator.js
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="8">
<title>QUnit Test Suite</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css" type="text/css" media="screen">
<script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<!-- Your project file goes here -->
<script type="text/javascript" src="../siapf.js"></script>
<!-- Your tests file goes here -->
<script type="text/javascript" src="siapfTest.js"></script>
</head>
<body>
<h1 id="qunit-header">QUnit Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>
function dvSiapf(siapf) {
var pad = "0000000";
var dv = null;
var dv1 = 0;
var dv2 = 0;
var tabela = [6, 8, 0, 2, 4, 7, 9, 1, 3, 5];
var digitos = [];
var somaCalculaDigito = 0;
var operacaoTemporaria;
siapf = "" + parseInt(siapf);
// siapf = pad.substring(0, pad.length - siapf.length) + siapf;
// siapf = siapf.substring(0, 7);
digitos = siapf.split('');
for (index = 0, z = digitos.length; index < z; index++) {
if (index % 2 === 0) {
somaCalculaDigito += parseInt(tabela[digitos[index]]);
} else {
somaCalculaDigito += parseInt(digitos[index]);
}
console.log("somaCalculaDigito: ");
console.log(somaCalculaDigito);
dv1 = somaCalculaDigito % 10;
};
console.log("Dv1: " + dv1);
operacaoTemporaria = dv1 + "" + siapf;
somaCalculaDigito = 0
digitos = operacaoTemporaria.split('');
for (var i = 0, z = digitos.length; i < z; i++) {
// digitos[i];
if (i % 2 == 0) {
somaCalculaDigito += parseInt(tabela[digitos[i]]);
} else {
somaCalculaDigito += parseInt(digitos[i]);
}
dv2 = somaCalculaDigito % 10;
};
pad = "00";
dv = dv1 + "" + dv2;
dv = pad.substring(0, pad.length - dv.length) + dv;
dv = dv.substring(0, 2);
return dv;
}
test('dvSiapf()', function() {
deepEqual(dvSiapf('0161138'), '37', '0161138-37');
deepEqual(dvSiapf('1003766'), '33', '1003766-33');
deepEqual(dvSiapf('0216997'), '19', '0216997-19');
deepEqual(dvSiapf('0217000'), '82', '0217000-82');
deepEqual(dvSiapf('0227448'), '95', '0227448-95');
deepEqual(dvSiapf('0230764'), '90', '0230764-90');
})
// // Let's test this function
// function isEven(val) {
// return val % 2 === 0;
// }
// test('isEven()', function() {
// ok(isEven(0), 'Zero is an even number');
// ok(isEven(2), 'So is two');
// ok(isEven(-4), 'So is negative four');
// ok(!isEven(1), 'One is not an even number');
// ok(!isEven(-7), 'Neither is negative seven');
// })
// test('test', function() {
// equal(0, false, 'true');
// deepEqual(0, false, 'false');
// equal(null, undefined, 'true');
// deepEqual(null, undefined, 'false');
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.