Skip to content

Instantly share code, notes, and snippets.

@gabrieljmj
Last active January 18, 2016 23:05
Show Gist options
  • Save gabrieljmj/96db6f612557f3a42e52 to your computer and use it in GitHub Desktop.
Save gabrieljmj/96db6f612557f3a42e52 to your computer and use it in GitHub Desktop.
PAS UEM NOTA
'use strict';
const isPower2 = function (n) {
while (true) {
n = n/2;
if (n < 1) {
return n == 0.5 ? true : false;
}
}
};
const getPower2Numbers = function (n) {
if (isPower2(n)) {
return [n];
}
let p = [],
k = true,
off = 0,
bigger = 0;
while (k) {
n--;
off++;
if (isPower2(n)) {
bigger = n;
p = getPower2Numbers(n);
}
if (bigger !== 0) {
if (isPower2(off)) {
k = false;
p.push(off);
} else {
p = p.concat(getPower2Numbers(off));
}
}
}
return p;
};
Array.prototype.contains = function (value) {
for (let k = 0, len = this.length; k < len; k++) {
if (this[k] == value) return true;
}
};
Array.prototype.isEqual = function (arr) {
if (arr.length != this.length || !(arr instanceof Array)) return false;
for (let k = 0, len = this.length; k < len; k++) {
if (this[k] instanceof Array && arr[k] instanceof Array) {
if (!this[k].isEqual(arr[k])) return false;
} else {
if (arr[k] !== this[k]) return false;
}
}
return true;
}
const calculatePasUemGrade = function (myResults, correctResults) {
let grade = 0;
for (let k = 0, len = correctResults.length; k < len; k++) {
let correctOnes = getPower2Numbers(correctResults[k]),
my = getPower2Numbers(myResults[k]),
questionGrade = 0,
end = false,
value = 6/correctOnes.length;
if (correctOnes.isEqual(my)) {
questionNote += 6;
} else {
for (let i = 0, _len = my.length; i < _len; i++) {
if (!correctOnes.contains(my[i])) {
questionGrade = 0;
end = true;
}
}
if (!end) {
for (let i = 0, _len = correctOnes.length; i < _len; i++) {
if (my.contains(correctOnes[i])) {
questionGrade += value;
}
}
}
}
grade += questionGrade;
}
return grade;
};
const myQuestionsValues = [30, 10, 4],
uemQuestionsValues = [30, 8, 1],
myGrade = calculatePasUemGrade(myQuestionsValues, uemQuestionsValues);
console.log('NOTA: ' + myGrade);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment