Questions:
- Convert Binary Numbers in Single Linked List to Number
- Count the number of Sundays for a given year
- Find circular primes less than n [Unsolved]
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 [Unsolved]
- Determines whether or not n is a perfect number (The easiest one in this list)
Solutions:
- Convert Binary Numbers in Single Linked List to Number
var getNumber = function(binary) {
let num = binary.data + "";
while (binary.next !== null) {
binary = binary.next;
//add the next element’s value onto the current binary
num += binary.data;
}
let tmp = "";
num.split("").reverse().forEach((x,i) => {
number = BigInt(Math.pow(2, i) * x) + BigInt(tmp);
});
return tmp.toString();
};
- Count the number of Sundays for a given year
var sundays = 0;
for (var month = 1; month<=12; month++) {
if (new Date(year, month-1, 1).getDay() == 0) {
sundays++;
}
}
return sundays