added solutions for 1 through 12
This commit is contained in:
parent
6b0e0e55e3
commit
10257c0934
|
@ -1,5 +1,5 @@
|
|||
const helloWorld = function() {
|
||||
return ''
|
||||
return 'Hello, World!'
|
||||
};
|
||||
|
||||
module.exports = helloWorld;
|
||||
|
|
|
@ -1,6 +1,33 @@
|
|||
const repeatString = function() {
|
||||
// const repeatString = function(string, num) {
|
||||
// // this function should return a repeated string
|
||||
// let tempString='';
|
||||
// if (num < 0) { // for less than zero condition
|
||||
// return 'ERROR';
|
||||
// }
|
||||
// if (num === 0) { // for zero condition
|
||||
// return tempString;
|
||||
// }
|
||||
// for (let i = 1; i <= num; i++){ // all other conditions
|
||||
// tempString +=string;
|
||||
// }
|
||||
// return tempString;
|
||||
// };
|
||||
|
||||
};
|
||||
//using recursion to repeat a string
|
||||
const repeatString = function (word, times) {
|
||||
if (times === 0){
|
||||
return '';
|
||||
}
|
||||
if (times < 0){
|
||||
return 'ERROR';
|
||||
}
|
||||
if (times === 1){
|
||||
return word;
|
||||
}
|
||||
|
||||
return word + repeatString(word, times - 1);
|
||||
}
|
||||
// repeatString(hello,1) output = hello
|
||||
// repeatString(hello,2) output = hello + hello
|
||||
// Do not edit below this line
|
||||
module.exports = repeatString;
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('repeatString', () => {
|
|||
test('repeats the string', () => {
|
||||
expect(repeatString('hey', 3)).toEqual('heyheyhey');
|
||||
});
|
||||
test.skip('repeats the string many times', () => {
|
||||
test('repeats the string many times', () => {
|
||||
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
|
||||
});
|
||||
test.skip('repeats the string 1 times', () => {
|
||||
test('repeats the string 1 times', () => {
|
||||
expect(repeatString('hey', 1)).toEqual('hey');
|
||||
});
|
||||
test.skip('repeats the string 0 times', () => {
|
||||
test('repeats the string 0 times', () => {
|
||||
expect(repeatString('hey', 0)).toEqual('');
|
||||
});
|
||||
test.skip('returns ERROR with negative numbers', () => {
|
||||
test('returns ERROR with negative numbers', () => {
|
||||
expect(repeatString('hey', -1)).toEqual('ERROR');
|
||||
});
|
||||
test.skip('repeats the string a random amount of times', function () {
|
||||
test('repeats the string a random amount of times', function () {
|
||||
/*The number is generated by using Math.random to get a value from between
|
||||
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
|
||||
equals a number between 0 to 999 (this number will change everytime you run
|
||||
|
@ -31,7 +31,7 @@ describe('repeatString', () => {
|
|||
was randomaly generated. */
|
||||
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
|
||||
});
|
||||
test.skip('works with blank strings', () => {
|
||||
test('works with blank strings', () => {
|
||||
expect(repeatString('', 10)).toEqual('');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
const reverseString = function(str) {
|
||||
|
||||
// let splitString = str.split(''); // turn string into an array of characters
|
||||
// let reverseArray = splitString.reverse(); // reverse array
|
||||
// let reversedString = reverseArray.join(''); // join characters into a string
|
||||
// return reversedString;
|
||||
return str.split('').reverse().join('');
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -5,14 +5,14 @@ describe('reverseString', () => {
|
|||
expect(reverseString('hello')).toEqual('olleh');
|
||||
});
|
||||
|
||||
test.skip('reverses multiple words', () => {
|
||||
test('reverses multiple words', () => {
|
||||
expect(reverseString('hello there')).toEqual('ereht olleh')
|
||||
})
|
||||
|
||||
test.skip('works with numbers and punctuation', () => {
|
||||
test('works with numbers and punctuation', () => {
|
||||
expect(reverseString('123! abc!')).toEqual('!cba !321')
|
||||
})
|
||||
test.skip('works with blank strings', () => {
|
||||
test('works with blank strings', () => {
|
||||
expect(reverseString('')).toEqual('')
|
||||
})
|
||||
});
|
||||
|
|
|
@ -1,6 +1,26 @@
|
|||
const removeFromArray = function() {
|
||||
const removeFromArray = function(...args) {
|
||||
const initialArgs = args[0];
|
||||
const finalArray = [];
|
||||
|
||||
initialArgs.forEach( (item) => {
|
||||
if (!args.includes(item)) {
|
||||
finalArray.push(item);
|
||||
console.log('item to push in ' + item);
|
||||
}
|
||||
})
|
||||
return finalArray;
|
||||
};
|
||||
|
||||
// three things to do when solving a problem
|
||||
// 1.understand the problem
|
||||
// 2.research and refine the problem
|
||||
// 3.psudocode the problem
|
||||
// iterate through the initial array looking at each item.
|
||||
// at each item, perform the following logic,
|
||||
//push every element into the new array unless it is included in the function arguments
|
||||
// else if there is not a match continue on
|
||||
// return the initial array with removed items if any
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = removeFromArray;
|
||||
|
|
|
@ -4,22 +4,22 @@ describe('removeFromArray', () => {
|
|||
test('removes a single value', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||
});
|
||||
test.skip('removes multiple values', () => {
|
||||
test('removes multiple values', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
||||
});
|
||||
test.skip('ignores non present values', () => {
|
||||
test('ignores non present values', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
test.skip('ignores non present values, but still works', () => {
|
||||
test('ignores non present values, but still works', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||
});
|
||||
test.skip('can remove all values', () => {
|
||||
test('can remove all values', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||
});
|
||||
test.skip('works with strings', () => {
|
||||
test('works with strings', () => {
|
||||
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
|
||||
});
|
||||
test.skip('only removes same type', () => {
|
||||
test('only removes same type', () => {
|
||||
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,27 @@
|
|||
const sumAll = function() {
|
||||
const sumAll = function(firstArg, secondArg) {
|
||||
let startCounter = firstArg;
|
||||
let stopCounter = secondArg;
|
||||
let sum = 0;
|
||||
|
||||
if (!(typeof firstArg === 'number') || !(typeof secondArg === 'number')){ //non-number case
|
||||
return "ERROR"
|
||||
}
|
||||
|
||||
if (startCounter < 0 || stopCounter < 0 ) // negative case
|
||||
return "ERROR"
|
||||
|
||||
if (startCounter > stopCounter){ // large number case
|
||||
for (let step = startCounter; step >= stopCounter; step--) {
|
||||
sum += step;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
else {
|
||||
for (let step = 1; step <= stopCounter; step++) {
|
||||
sum += step;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('sumAll', () => {
|
|||
test('sums numbers within the range', () => {
|
||||
expect(sumAll(1, 4)).toEqual(10);
|
||||
});
|
||||
test.skip('works with large numbers', () => {
|
||||
test('works with large numbers', () => {
|
||||
expect(sumAll(1, 4000)).toEqual(8002000);
|
||||
});
|
||||
test.skip('works with larger number first', () => {
|
||||
test('works with larger number first', () => {
|
||||
expect(sumAll(123, 1)).toEqual(7626);
|
||||
});
|
||||
test.skip('returns ERROR with negative numbers', () => {
|
||||
test('returns ERROR with negative numbers', () => {
|
||||
expect(sumAll(-10, 4)).toEqual('ERROR');
|
||||
});
|
||||
test.skip('returns ERROR with non-number parameters', () => {
|
||||
test('returns ERROR with non-number parameters', () => {
|
||||
expect(sumAll(10, "90")).toEqual('ERROR');
|
||||
});
|
||||
test.skip('returns ERROR with non-number parameters', () => {
|
||||
test('returns ERROR with non-number parameters', () => {
|
||||
expect(sumAll(10, [90, 1])).toEqual('ERROR');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
const leapYears = function() {
|
||||
|
||||
const leapYears = function(year) {
|
||||
if ( (year % 4 == 0) && (year % 100 != 0 ) || (year % 400 == 0)) {
|
||||
return true;
|
||||
}
|
||||
return false; // not a leap year
|
||||
};
|
||||
|
||||
/*
|
||||
> Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). (Yes, it's all pretty confusing)
|
||||
*/
|
||||
// Do not edit below this line
|
||||
module.exports = leapYears;
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('leapYears', () => {
|
|||
test('works with non century years', () => {
|
||||
expect(leapYears(1996)).toBe(true);
|
||||
});
|
||||
test.skip('works with non century years', () => {
|
||||
test('works with non century years', () => {
|
||||
expect(leapYears(1997)).toBe(false);
|
||||
});
|
||||
test.skip('works with ridiculously futuristic non century years', () => {
|
||||
test('works with ridiculously futuristic non century years', () => {
|
||||
expect(leapYears(34992)).toBe(true);
|
||||
});
|
||||
test.skip('works with century years', () => {
|
||||
test('works with century years 1900', () => {
|
||||
expect(leapYears(1900)).toBe(false);
|
||||
});
|
||||
test.skip('works with century years', () => {
|
||||
test('works with century years 1600', () => {
|
||||
expect(leapYears(1600)).toBe(true);
|
||||
});
|
||||
test.skip('works with century years', () => {
|
||||
test('works with century years', () => {
|
||||
expect(leapYears(700)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
const ftoc = function() {
|
||||
|
||||
const ftoc = function(value) {
|
||||
let convertedValue = (value - 32) * (5/9);
|
||||
let rounded = Math.round(convertedValue * 10) / 10
|
||||
return rounded;
|
||||
};
|
||||
|
||||
const ctof = function() {
|
||||
|
||||
//[°C] = ([°F] − 32) × 5⁄9
|
||||
const ctof = function(value) {
|
||||
let convertedValue = value * (9/5) + 32;
|
||||
let rounded = Math.round(convertedValue * 10) / 10
|
||||
return rounded;
|
||||
};
|
||||
|
||||
// [°F] = [°C] × 9⁄5 + 32
|
||||
// Do not edit below this line
|
||||
module.exports = {
|
||||
ftoc,
|
||||
|
|
|
@ -4,22 +4,22 @@ describe('ftoc', () => {
|
|||
test('works', () => {
|
||||
expect(ftoc(32)).toEqual(0);
|
||||
});
|
||||
test.skip('rounds to 1 decimal', () => {
|
||||
test('rounds to 1 decimal', () => {
|
||||
expect(ftoc(100)).toEqual(37.8);
|
||||
});
|
||||
test.skip('works with negatives', () => {
|
||||
test('works with negatives', () => {
|
||||
expect(ftoc(-100)).toEqual(-73.3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ctof', () => {
|
||||
test.skip('works', () => {
|
||||
test('works', () => {
|
||||
expect(ctof(0)).toEqual(32);
|
||||
});
|
||||
test.skip('rounds to 1 decimal', () => {
|
||||
test('rounds to 1 decimal', () => {
|
||||
expect(ctof(73.2)).toEqual(163.8);
|
||||
});
|
||||
test.skip('works with negatives', () => {
|
||||
test('works with negatives', () => {
|
||||
expect(ctof(-10)).toEqual(14);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,26 +1,47 @@
|
|||
const add = function() {
|
||||
|
||||
const add = function(input1, input2) {
|
||||
return input1+input2;
|
||||
};
|
||||
|
||||
const subtract = function() {
|
||||
|
||||
const subtract = function(input1, input2) {
|
||||
return input1 - input2;
|
||||
};
|
||||
|
||||
const sum = function() {
|
||||
|
||||
const sum = function(input1) {
|
||||
let total = 0;
|
||||
input1.forEach( (item) => {
|
||||
total +=item;
|
||||
});
|
||||
return total;
|
||||
};
|
||||
|
||||
const multiply = function() {
|
||||
|
||||
const multiply = function(input1) {
|
||||
let total = 1;
|
||||
input1.forEach( (item) =>{
|
||||
total *=item;
|
||||
});
|
||||
return total;
|
||||
};
|
||||
|
||||
const power = function() {
|
||||
|
||||
const power = function(input1, input2) {
|
||||
//return Math.pow(input1, input2);
|
||||
return input1 ** input2
|
||||
};
|
||||
|
||||
const factorial = function() {
|
||||
const factorial = function(input1) {
|
||||
var result = input1;
|
||||
if (input1 === 0 || input1 === 1){
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (input1 > 1){
|
||||
input1--;
|
||||
result = result * input1;
|
||||
}
|
||||
return result;
|
||||
|
||||
};
|
||||
//2!
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = {
|
||||
|
|
|
@ -5,73 +5,73 @@ describe('add', () => {
|
|||
expect(calculator.add(0,0)).toBe(0);
|
||||
});
|
||||
|
||||
test.skip('adds 2 and 2', () => {
|
||||
test('adds 2 and 2', () => {
|
||||
expect(calculator.add(2,2)).toBe(4);
|
||||
});
|
||||
|
||||
test.skip('adds positive numbers', () => {
|
||||
test('adds positive numbers', () => {
|
||||
expect(calculator.add(2,6)).toBe(8);
|
||||
});
|
||||
});
|
||||
|
||||
describe('subtract', () => {
|
||||
test.skip('subtracts numbers', () => {
|
||||
test('subtracts numbers', () => {
|
||||
expect(calculator.subtract(10,4)).toBe(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sum', () => {
|
||||
test.skip('computes the sum of an empty array', () => {
|
||||
test('computes the sum of an empty array', () => {
|
||||
expect(calculator.sum([])).toBe(0);
|
||||
});
|
||||
|
||||
test.skip('computes the sum of an array of one number', () => {
|
||||
test('computes the sum of an array of one number', () => {
|
||||
expect(calculator.sum([7])).toBe(7);
|
||||
});
|
||||
|
||||
test.skip('computes the sum of an array of two numbers', () => {
|
||||
test('computes the sum of an array of two numbers', () => {
|
||||
expect(calculator.sum([7,11])).toBe(18);
|
||||
});
|
||||
|
||||
test.skip('computes the sum of an array of many numbers', () => {
|
||||
test('computes the sum of an array of many numbers', () => {
|
||||
expect(calculator.sum([1,3,5,7,9])).toBe(25);
|
||||
});
|
||||
});
|
||||
|
||||
describe('multiply', () => {
|
||||
test.skip('multiplies two numbers', () => {
|
||||
test('multiplies two numbers', () => {
|
||||
expect(calculator.multiply([2,4])).toBe(8);
|
||||
});
|
||||
|
||||
test.skip('multiplies several numbers', () => {
|
||||
test('multiplies several numbers', () => {
|
||||
expect(calculator.multiply([2,4,6,8,10,12,14])).toBe(645120);
|
||||
});
|
||||
});
|
||||
|
||||
describe('power', () => {
|
||||
test.skip('raises one number to the power of another number', () => {
|
||||
test('raises one number to the power of another number', () => {
|
||||
expect(calculator.power(4,3)).toBe(64); // 4 to third power is 64
|
||||
});
|
||||
});
|
||||
|
||||
describe('factorial', () => {
|
||||
test.skip('computes the factorial of 0', () => {
|
||||
test('computes the factorial of 0', () => {
|
||||
expect(calculator.factorial(0)).toBe(1); // 0! = 1
|
||||
});
|
||||
|
||||
test.skip('computes the factorial of 1', () => {
|
||||
test('computes the factorial of 1', () => {
|
||||
expect(calculator.factorial(1)).toBe(1);
|
||||
});
|
||||
|
||||
test.skip('computes the factorial of 2', () => {
|
||||
test('computes the factorial of 2', () => {
|
||||
expect(calculator.factorial(2)).toBe(2);
|
||||
});
|
||||
|
||||
test.skip('computes the factorial of 5', () => {
|
||||
test('computes the factorial of 5', () => {
|
||||
expect(calculator.factorial(5)).toBe(120);
|
||||
});
|
||||
|
||||
test.skip('computes the factorial of 10', () => {
|
||||
test('computes the factorial of 10', () => {
|
||||
expect(calculator.factorial(10)).toBe(3628800);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -11,6 +11,13 @@ A palindrome is a string that is spelled the same both forwards and backwards, u
|
|||
- Animal loots foliated detail of stool lamina.
|
||||
- A nut for a jar of tuna.
|
||||
|
||||
|
||||
/*
|
||||
1. understand the problem
|
||||
2. research and rewrite the problem
|
||||
3. pseudocode problem
|
||||
|
||||
*/
|
||||
```javascript
|
||||
palindromes('racecar') // true
|
||||
palindromes('tacos') // false
|
||||
|
|
|
@ -1,6 +1,21 @@
|
|||
const palindromes = function () {
|
||||
const palindromes = function (initialWord) {
|
||||
let re = /[^A-Za-z0-9]/g;
|
||||
let cleanString = initialWord.replace(re, '').toLowerCase();
|
||||
let reverseString = cleanString.split('').reverse().join('');
|
||||
console.log('old word: ' +cleanString);
|
||||
console.log('new word: ' +reverseString);
|
||||
return cleanString === reverseString;
|
||||
|
||||
|
||||
};
|
||||
|
||||
// psuedocode
|
||||
//A palindrome is a string that is spelled the same both forwards and backwards,
|
||||
// write logic to strip out the punctuation or word breaks
|
||||
// write logic that will compare the one character at a time for a match. if there is a mismatch, then it is not a palindromes.
|
||||
|
||||
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = palindromes;
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('palindromes', () => {
|
|||
test('works with single words', () => {
|
||||
expect(palindromes('racecar')).toBe(true);
|
||||
});
|
||||
test.skip('works with punctuation ', () => {
|
||||
test('works with punctuation ', () => {
|
||||
expect(palindromes('racecar!')).toBe(true);
|
||||
});
|
||||
test.skip('works with upper-case letters ', () => {
|
||||
test('works with upper-case letters ', () => {
|
||||
expect(palindromes('Racecar!')).toBe(true);
|
||||
});
|
||||
test.skip('works with multiple words', () => {
|
||||
test('works with multiple words', () => {
|
||||
expect(palindromes('A car, a man, a maraca.')).toBe(true);
|
||||
});
|
||||
test.skip('works with multiple words', () => {
|
||||
test('works with multiple words', () => {
|
||||
expect(palindromes('Animal loots foliated detail of stool lamina.')).toBe(true);
|
||||
});
|
||||
test.skip('doesn\'t just always return true', () => {
|
||||
test('doesn\'t just always return true', () => {
|
||||
expect(palindromes('ZZZZ car, a man, a maraca.')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,32 @@
|
|||
const fibonacci = function() {
|
||||
const fibonacci = function(n) {
|
||||
// if (number === 0) {
|
||||
// return 0;
|
||||
// }
|
||||
// if (number === 1) {
|
||||
// return 1;
|
||||
// }
|
||||
// let next = 1;
|
||||
// let prev = 0;
|
||||
// for (let i = 0; i < number; i++){
|
||||
// let temp = next;
|
||||
// next = prev + next;
|
||||
// prev = temp;
|
||||
// }
|
||||
// return prev;
|
||||
if (n < 0)
|
||||
return "OOPS"
|
||||
if (n <= 1)
|
||||
return parseInt(n);
|
||||
return fibonacci(n-1) + fibonacci(n-2);
|
||||
|
||||
// fibonacci(3)
|
||||
/*
|
||||
fib(1) + fib (1) = 1 + 1
|
||||
*/
|
||||
};
|
||||
//fibonacci number value where f0 = 0 , f1 = 1 and
|
||||
// fn = f(n-1) + f(n-2)
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = fibonacci;
|
||||
|
|
|
@ -4,28 +4,28 @@ describe('fibonacci', () => {
|
|||
test('4th fibonacci number is 3', () => {
|
||||
expect(fibonacci(4)).toBe(3);
|
||||
});
|
||||
test.skip('6th fibonacci number is 8', () => {
|
||||
test('6th fibonacci number is 8', () => {
|
||||
expect(fibonacci(6)).toBe(8);
|
||||
});
|
||||
test.skip('10th fibonacci number is 55', () => {
|
||||
test('10th fibonacci number is 55', () => {
|
||||
expect(fibonacci(10)).toBe(55);
|
||||
});
|
||||
test.skip('15th fibonacci number is 610', () => {
|
||||
test('15th fibonacci number is 610', () => {
|
||||
expect(fibonacci(15)).toBe(610);
|
||||
});
|
||||
test.skip('25th fibonacci number is 75025', () => {
|
||||
test('25th fibonacci number is 75025', () => {
|
||||
expect(fibonacci(25)).toBe(75025);
|
||||
});
|
||||
test.skip('doesn\'t accept negatives', () => {
|
||||
test('doesn\'t accept negatives', () => {
|
||||
expect(fibonacci(-25)).toBe("OOPS");
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("1")).toBe(1);
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("2")).toBe(1);
|
||||
});
|
||||
test.skip('DOES accept strings', () => {
|
||||
test('DOES accept strings', () => {
|
||||
expect(fibonacci("8")).toBe(21);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
const getTheTitles = function() {
|
||||
|
||||
const getTheTitles = function(payload) {
|
||||
// const temparray = [];
|
||||
// payload.forEach( (item) =>{
|
||||
// temparray.push(item['title']);
|
||||
// })
|
||||
// return temparray;
|
||||
return payload.map(book => book.title);
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,6 +1,50 @@
|
|||
const findTheOldest = function() {
|
||||
|
||||
const findTheOldest = function(array) {
|
||||
let oldestPersonObj = {};
|
||||
let previousPersonAge = 0;
|
||||
array.forEach( (element,index) => {
|
||||
let currentPersonAge = getCurrentAge(element);
|
||||
if (currentPersonAge > previousPersonAge ){
|
||||
oldestPersonObj = element;
|
||||
previousPersonAge = currentPersonAge;
|
||||
}
|
||||
});
|
||||
console.log('Oldest living person is ' +oldestPersonObj.name + ' age ' +getCurrentAge(oldestPersonObj));
|
||||
return oldestPersonObj
|
||||
};
|
||||
|
||||
const getCurrentAge = function (person) {
|
||||
|
||||
if (person.yearOfDeath){
|
||||
return (person.yearOfDeath - person.yearOfBirth);
|
||||
}
|
||||
else {
|
||||
let todayDate = new Date();
|
||||
return (todayDate.getFullYear() - person.yearOfBirth);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
const people = [
|
||||
{
|
||||
name: "Carly",
|
||||
yearOfBirth: 1942,
|
||||
yearOfDeath: 1970,
|
||||
},
|
||||
{
|
||||
name: "Ray",
|
||||
yearOfBirth: 1962,
|
||||
yearOfDeath: 2011,
|
||||
},
|
||||
{
|
||||
name: "Jane",
|
||||
yearOfBirth: 1912,
|
||||
yearOfDeath: 1941,
|
||||
},
|
||||
]
|
||||
*/
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = findTheOldest;
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ describe('findTheOldest', () => {
|
|||
]
|
||||
expect(findTheOldest(people).name).toBe('Ray');
|
||||
});
|
||||
test.skip('finds the oldest person if someone is still living', () => {
|
||||
test('finds the oldest person if someone is still living', () => {
|
||||
const people = [
|
||||
{
|
||||
name: "Carly",
|
||||
|
@ -40,7 +40,7 @@ describe('findTheOldest', () => {
|
|||
]
|
||||
expect(findTheOldest(people).name).toBe('Ray');
|
||||
});
|
||||
test.skip('finds the oldest person if the OLDEST is still living', () => {
|
||||
test('finds the oldest person if the OLDEST is still living', () => {
|
||||
const people = [
|
||||
{
|
||||
name: "Carly",
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -14,11 +14,11 @@
|
|||
},
|
||||
"homepage": "https://github.com/TheOdinProject/javascript-exercises#readme",
|
||||
"devDependencies": {
|
||||
"jest": "^26.6.3",
|
||||
"jest-cli": "^26.6.3",
|
||||
"eslint": "^7.26.0",
|
||||
"eslint-config-airbnb-base": "^14.2.1",
|
||||
"eslint-plugin-import": "^2.22.1"
|
||||
"eslint-plugin-import": "^2.22.1",
|
||||
"jest": "^26.6.3",
|
||||
"jest-cli": "^26.6.3"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
|
@ -26,7 +26,7 @@
|
|||
"eslintConfig": {
|
||||
"root": true
|
||||
},
|
||||
"jest": {
|
||||
"jest": {
|
||||
"testPathIgnorePatterns": [
|
||||
"generator-exercise/"
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue