calculator & palindromes projects

This commit is contained in:
vfonsah 2020-09-06 23:35:05 +01:00
parent 7f6b7dc5b1
commit b1f81c30b2
2 changed files with 51 additions and 22 deletions

View File

@ -1,25 +1,43 @@
function add () {
function add(a, b) {
return a + b;
}
function subtract () {
function subtract(a, b) {
return a - b;
}
function sum () {
function sum(arr) {
let sum = 0;
arr.forEach((num) => {
sum += num;
});
return arr.length == 0 ? 0 : sum;
}
function multiply () {
function multiply(arr) {
let product = 1;
arr.forEach((num) => {
product *= num;
});
return arr.length == 0 ? 0 : product;
}
function power() {
function power(num, pow) {
let exponent = 1
for (let i = 0; i < pow; i++) {
exponent *= num;
}
return exponent;
}
function factorial() {
function factorial(n) {
let facto = 1;
for (let i = 0; i < n; i++) {
facto = n * factorial(n-1);
}
return n == 0 || n == 1 ? 1 : facto;
}
module.exports = {
@ -28,5 +46,5 @@ module.exports = {
sum,
multiply,
power,
factorial
}
factorial,
};

View File

@ -1,5 +1,16 @@
const palindromes = function() {
const palindromes = function (str) {
let strippedStr = str.toLowerCase().replace(
/(~|`|!|@|#|$|%|^|&|\*|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|\.|>|\?|\/|\\|\||-|_|\+| |=)/g,
""
);
let splitStr = strippedStr.split("");
let reversedStr = [];
for (let i = splitStr.length - 1; i >= 0; i--) {
reversedStr.push(splitStr[i]);
}
// return [splitStr, reversedStr, JSON.stringify(splitStr), JSON.stringify(reversedStr)];
return JSON.stringify(splitStr) === JSON.stringify(reversedStr) ? true : false;
};
module.exports = palindromes
module.exports = palindromes;