solutions

This commit is contained in:
ArtitRise 2022-09-06 19:18:39 +07:00
parent 0747078d97
commit c3eae7f3d4
26 changed files with 7773 additions and 111 deletions

View File

@ -1,5 +1,5 @@
const helloWorld = function() {
return ''
return 'Hello, World!'
};
module.exports = helloWorld;

View File

@ -1,5 +1,12 @@
const repeatString = function() {
const repeatString = function (word, num) {
let output = "";
for (i = 1; i <= num; i++) {
output += word;
}
if (num < 0) {
output = "ERROR";
}
return output;
};
// Do not edit below this line

View File

@ -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 randomly 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('');
});
});

View File

@ -1,5 +1,10 @@
const reverseString = function() {
const reverseString = function (word) {
let charArray = word.split("");
let revArray = [];
for (i = charArray.length - 1; i >= 0; i--) {
revArray.push(charArray[i]);
}
return revArray.join('')
};
// Do not edit below this line

View File

@ -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('')
})
});

View File

@ -1,5 +1,13 @@
const removeFromArray = function() {
const removeFromArray = function (array, ...toRemove) {
let result = [...array];
for (i = 0; i < toRemove.length; i++) {
let index = result.indexOf(toRemove[i]);
if (index >= 0) {
result.splice(index, 1);
}
}
return result;
};
// Do not edit below this line

View File

@ -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]);
});
});

View File

@ -1,5 +1,18 @@
const sumAll = function() {
const sumAll = function (number1, number2) {
let number1State = !Number.isInteger(number1) || (number1 < 0);
let number2State = !Number.isInteger(number2) || (number2 < 0);
if (number1State || number2State) {
return "ERROR";
} else {
let max = number1 >= number2 ? number1 : number2;
let min = number1 >= number2 ? number2 : number1;
let result =0
for (i=min;i<=max;i++) {
result += i
}
return result
}
};
// Do not edit below this line

View File

@ -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');
});
});

View File

@ -1,5 +1,13 @@
const leapYears = function() {
const leapYears = function(year) {
if (year % 400 == 0) {
return true
} else if (year % 100 == 0) {
return false
} else if (year % 4 ==0) {
return true
} else {
return false
}
};
// Do not edit below this line

View File

@ -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', () => {
expect(leapYears(1900)).toBe(false);
});
test.skip('works with century years', () => {
test('works with century years', () => {
expect(leapYears(1600)).toBe(true);
});
test.skip('works with century years', () => {
test('works with century years', () => {
expect(leapYears(700)).toBe(false);
});
});

View File

@ -1,13 +1,15 @@
const ftoc = function() {
const ftoc = function (temp) {
let celcius = ((temp - 32) * 5) / 9;
return Number(celcius.toFixed(1));
};
const ctof = function() {
const ctof = function (temp) {
let fahrenheit = (temp * 9) / 5 + 32;
return Number(fahrenheit.toFixed(1));
};
// Do not edit below this line
module.exports = {
ftoc,
ctof
ctof,
};

View File

@ -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);
});
});

View File

@ -1,25 +1,44 @@
const add = function() {
const add = function (a, b) {
let result = a + b;
return result;
};
const subtract = function() {
const subtract = function (a, b) {
let result = a - b;
return result;
};
const sum = function() {
const sum = function (array) {
let result = 0;
for (i = 0; i < array.length; i++) {
result += array[i];
}
return result;
};
const multiply = function() {
const multiply = function (array) {
let result = 1;
for (i = 0; i < array.length; i++) {
result *= array[i];
}
return result;
};
const power = function() {
const power = function (a, b) {
let result = a ** b;
return result;
};
const factorial = function() {
const factorial = function (val) {
let result = 1;
if (val == 0) {
result = 1;
} else {
for (i = 1; i <= val; i++) {
result *= i;
}
}
return result;
};
// Do not edit below this line
@ -29,5 +48,5 @@ module.exports = {
sum,
multiply,
power,
factorial
factorial,
};

View File

@ -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);
});
});

View File

@ -1,5 +1,42 @@
const palindromes = function () {
const palindromes = function (val) {
/*
let separatedBy = [" ", "!", ".", ","];
let separated;
let reversed;
let joined = val;
let removed
for (i = 0; i < separatedBy.length; i++) {
let temp = joined.split(separatedBy[i]);
joined = temp.join("");
}
removed = joined.split("");
for (i = removed.length - 1; i >= 0; i--) {
reversed.push(separated[i]);
}
if (separated === reversed) {
return true;
} else {
return false;
}
*/
let separatedBy = [" ", "!", ".", ","];
let separated = val.toLowerCase().split("");
let reversed = [];
for (i = 0; i < separatedBy.length; i++) {
separated = separated.filter(function (d) {
return d != separatedBy[i];
});
}
for (i = separated.length - 1; i >= 0; i--) {
reversed.push(separated[i]);
}
if (separated.join("") === reversed.join("")) {
return true;
} else {
return false;
}
};
// Do not edit below this line

View File

@ -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 maracaz.')).toBe(false);
});
});

View File

@ -1,5 +1,22 @@
const fibonacci = function() {
const fibonacci = function (d) {
let order = +d;
if (order <= 0) {
return "OOPS";
} else {
if (order === 1) {
return 1;
} else {
let prev = 0;
let next = 1;
let result;
for (i = 2; i <= order; i++) {
result = prev + next;
prev = next;
next = result;
}
return result
}
}
};
// Do not edit below this line

View File

@ -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);
});
});

View File

@ -1,5 +1,9 @@
const getTheTitles = function() {
const getTheTitles = function (array) {
let titles = [];
for (i = 0; i < array.length; i++) {
titles.push(array[i].title);
}
return titles
};
// Do not edit below this line

View File

@ -1,5 +1,21 @@
const findTheOldest = function() {
const findTheOldest = function (array) {
let resultYearOfDeath
let oldest = array.reduce(function (result, choice) {
if (result.yearOfDeath === undefined) {
let date = new Date();
resultYearOfDeath = date.getUTCFullYear();
} else {
resultYearOfDeath = result.yearOfDeath
}
let resultAge = resultYearOfDeath - result.yearOfBirth;
let choiceAge = choice.yearOfDeath - choice.yearOfBirth;
choiceAge > resultAge ? result = choice : result = result
return result;
});
return oldest
};
// Do not edit below this line

View File

@ -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",

View File

@ -1,5 +1,37 @@
const caesar = function() {
const caesar = function (str, encode) {
let arr = str.split("");
let arrToCode = arr.map(function (c) {
return c.charCodeAt();
});
encode = encode % 26;
let encodedArr = arrToCode.map(function (num) {
if (97 <= num && num <= 122) {
if (num + encode > 122) {
return 97 + (num + encode - 123);
} else if (num + encode < 97) {
return 122 + (num + encode - 96);
} else {
return num + encode;
}
} else if (65 <= num && num <= 90) {
if (num + encode > 90) {
return 65 + (num + encode - 91);
} else if (num + encode < 65) {
return 90 + (num + encode - 64);
} else {
return num + encode;
}
} else {
return num;
}
});
let encodedChar = encodedArr.map(function (d) {
return String.fromCharCode(d);
});
return encodedChar.join("");
};
// Do not edit below this line

View File

@ -3,21 +3,21 @@ const caesar = require('./caesar')
test('works with single letters', () => {
expect(caesar('A', 1)).toBe('B');
});
test.skip('works with words', () => {
test('works with words', () => {
expect(caesar('Aaa', 1)).toBe('Bbb');
});
test.skip('works with phrases', () => {
test('works with phrases', () => {
expect(caesar('Hello, World!', 5)).toBe('Mjqqt, Btwqi!');
});
test.skip('works with negative shift', () => {
test('works with negative shift', () => {
expect(caesar('Mjqqt, Btwqi!', -5)).toBe('Hello, World!');
});
test.skip('wraps', () => {
test('wraps', () => {
expect(caesar('Z', 1)).toBe('A');
});
test.skip('works with large shift factors', () => {
test('works with large shift factors', () => {
expect(caesar('Hello, World!', 75)).toBe('Ebiil, Tloia!');
});
test.skip('works with large negative shift factors', () => {
test('works with large negative shift factors', () => {
expect(caesar('Hello, World!', -29)).toBe('Ebiil, Tloia!');
});

7508
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -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/"
]