Compare commits
	
		
			37 Commits
		
	
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								 | 
						9eb942909e | |
| 
							
							
								 | 
						77c5d64b37 | |
| 
							
							
								 | 
						c4301bbd9c | |
| 
							
							
								 | 
						04ced679a3 | |
| 
							
							
								 | 
						adf2bf93bc | |
| 
							
							
								 | 
						0f57a2afcd | |
| 
							
							
								 | 
						44a95b8e89 | |
| 
							
							
								 | 
						171ac956cf | |
| 
							
							
								 | 
						2518281b10 | |
| 
							
							
								 | 
						e94742d58c | |
| 
							
							
								 | 
						6bafdb5094 | |
| 
							
							
								 | 
						c037ad5bf5 | |
| 
							
							
								 | 
						b52b2ace26 | |
| 
							
							
								 | 
						10cb98c038 | |
| 
							
							
								 | 
						db998d7279 | |
| 
							
							
								 | 
						4b13066369 | |
| 
							
							
								 | 
						7df108c5de | |
| 
							
							
								 | 
						de40b279df | |
| 
							
							
								 | 
						c09df52d6a | |
| 
							
							
								 | 
						8150be944b | |
| 
							
							
								 | 
						5b5687862a | |
| 
							
							
								 | 
						56ebfc0f7d | |
| 
							
							
								 | 
						4fca2e249c | |
| 
							
							
								 | 
						fda681965e | |
| 
							
							
								 | 
						a1c56276ea | |
| 
							
							
								 | 
						085fd89ec8 | |
| 
							
							
								 | 
						06e90f66c8 | |
| 
							
							
								 | 
						8546794b47 | |
| 
							
							
								 | 
						ac4d2a94e5 | |
| 
							
							
								 | 
						276b60a3c3 | |
| 
							
							
								 | 
						435c1d3779 | |
| 
							
							
								 | 
						9916e343a3 | |
| 
							
							
								 | 
						cced031981 | |
| 
							
							
								 | 
						2d6c505096 | |
| 
							
							
								 | 
						a5a0284ac1 | |
| 
							
							
								 | 
						ce0b9fade1 | |
| 
							
							
								 | 
						749a48adee | 
| 
						 | 
				
			
			@ -1,5 +1,29 @@
 | 
			
		|||
const caesar = function() {
 | 
			
		||||
const caesar = function(string, shiftValue) {
 | 
			
		||||
  return string
 | 
			
		||||
    .split("")
 | 
			
		||||
    .map(char => shift(char, shiftValue))
 | 
			
		||||
    .join("");
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const isLetter = code => {
 | 
			
		||||
  return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This function implements a version of the modulo operator
 | 
			
		||||
// that returns the smallest positive remainder even for negative inputs.
 | 
			
		||||
// See this link for details:
 | 
			
		||||
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
 | 
			
		||||
const mod = (n, m) => (n % m + m) % m;
 | 
			
		||||
 | 
			
		||||
const shift = (char, shiftValue) => {
 | 
			
		||||
  const code = char.charCodeAt();
 | 
			
		||||
 | 
			
		||||
  if (isLetter(code)) {
 | 
			
		||||
    const base = code < 97 ? 65 : 97;
 | 
			
		||||
    const shiftedCode = mod(code + shiftValue - base, 26) + base;
 | 
			
		||||
    return String.fromCharCode(shiftedCode);
 | 
			
		||||
  }
 | 
			
		||||
  return char;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = caesar;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,25 +1,41 @@
 | 
			
		|||
const add = function() {
 | 
			
		||||
	
 | 
			
		||||
const add = function(a, b) {
 | 
			
		||||
  return a + b;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const subtract = function() {
 | 
			
		||||
	
 | 
			
		||||
const subtract = function(a, b) {
 | 
			
		||||
  return a - b;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const sum = function() {
 | 
			
		||||
	
 | 
			
		||||
const sum = function(array) {
 | 
			
		||||
  return array.reduce((total, current) => total + current, 0);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const multiply = function() {
 | 
			
		||||
 | 
			
		||||
const multiply = function(array) {
 | 
			
		||||
    return array.length
 | 
			
		||||
      ? array.reduce((accumulator, nextItem) => accumulator * nextItem)
 | 
			
		||||
      : 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const power = function() {
 | 
			
		||||
	
 | 
			
		||||
const power = function(a, b) {
 | 
			
		||||
  return Math.pow(a, b);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const factorial = function() {
 | 
			
		||||
	
 | 
			
		||||
const factorial = function(n) {
 | 
			
		||||
  if (n === 0) return 1;
 | 
			
		||||
  let product = 1;
 | 
			
		||||
  for (let i = n; i > 0; i--) {
 | 
			
		||||
    product *= i;
 | 
			
		||||
  }
 | 
			
		||||
  return product;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// This is another implementation of Factorial that uses recursion
 | 
			
		||||
// THANKS to @ThirtyThreeB!
 | 
			
		||||
const recursiveFactorial = function(n) {
 | 
			
		||||
  if (n === 0) {
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  return n * recursiveFactorial (n-1);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,14 @@
 | 
			
		|||
const fibonacci = function() {
 | 
			
		||||
 | 
			
		||||
const fibonacci = function(count) {
 | 
			
		||||
  if (count < 0) return "OOPS";
 | 
			
		||||
  if (count === 0) return 0;
 | 
			
		||||
  let a = 0;
 | 
			
		||||
  let b = 1;
 | 
			
		||||
  for (let i = 1; i < count; i++) {
 | 
			
		||||
    const temp = b;
 | 
			
		||||
    b = a + b;
 | 
			
		||||
    a = temp;
 | 
			
		||||
  }
 | 
			
		||||
  return b;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = fibonacci;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,16 @@
 | 
			
		|||
const findTheOldest = function() {
 | 
			
		||||
const findTheOldest = function(array) {
 | 
			
		||||
  return array.reduce((oldest, currentPerson) => {
 | 
			
		||||
    const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
 | 
			
		||||
    const currentAge = getAge(currentPerson.yearOfBirth, currentPerson.yearOfDeath);
 | 
			
		||||
    return oldestAge < currentAge ? currentPerson : oldest;
 | 
			
		||||
  })
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const getAge = function(birth, death) {
 | 
			
		||||
  if (!death) {
 | 
			
		||||
    death = new Date().getFullYear();
 | 
			
		||||
  }
 | 
			
		||||
  return death - birth;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = findTheOldest;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
const getTheTitles = function() {
 | 
			
		||||
 | 
			
		||||
const getTheTitles = function(array) {
 | 
			
		||||
  return array.map(book => book.title);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = getTheTitles;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
const helloWorld = function() {
 | 
			
		||||
  return ''
 | 
			
		||||
  return 'Hello, World!';
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = helloWorld;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
const leapYears = function() {
 | 
			
		||||
 | 
			
		||||
const leapYears = function(year) {
 | 
			
		||||
  return year % 4 === 0 && ( year % 100 !== 0 || year % 400 === 0);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = leapYears;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											
												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"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,14 @@
 | 
			
		|||
const palindromes = function () {
 | 
			
		||||
 | 
			
		||||
// Non regex
 | 
			
		||||
const palindromes = function (string) {
 | 
			
		||||
	let alphabet = 'abcdefghijklmnopqrstuvwxyz'; 				//Create a variable that holds all the letters of the alphabet
 | 
			
		||||
	const cleanedString = string 						// Convert to lowercase, split, & filter only letters, rejoin as new const
 | 
			
		||||
		.toLowerCase()
 | 
			
		||||
		.split('')
 | 
			
		||||
		.filter((letter) => alphabet.includes(letter))
 | 
			
		||||
		.join('');
 | 
			
		||||
	const reversedString = cleanedString.split('').reverse().join(''); 	//Create a new const that holds reversed string
 | 
			
		||||
	return cleanedString === reversedString; 				//Compare cleanedString & reversedString which returns true/false
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Do not edit below this line
 | 
			
		||||
module.exports = palindromes;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,21 @@
 | 
			
		|||
function pigLatin(string) {
 | 
			
		||||
const pigLatin = function(string) {
 | 
			
		||||
	return string
 | 
			
		||||
	  .split(" ")
 | 
			
		||||
	  .map(word => {
 | 
			
		||||
		const index = firstVowelIndex(word);
 | 
			
		||||
		const beginning = word.slice(0, index);
 | 
			
		||||
		const ending = word.slice(index);
 | 
			
		||||
		return `${ending}${beginning}ay`;
 | 
			
		||||
	  })
 | 
			
		||||
	  .join(" ");
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
const firstVowelIndex = function(string) {
 | 
			
		||||
	const vowels = string.match(/[aeiou]/g);
 | 
			
		||||
	if (vowels[0] == "u" && string[string.indexOf(vowels[0]) - 1] == "q") {
 | 
			
		||||
	  return string.indexOf(vowels[1]);
 | 
			
		||||
	}
 | 
			
		||||
	return string.indexOf(vowels[0]);
 | 
			
		||||
};
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  module.exports = pigLatin;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,28 @@
 | 
			
		|||
const removeFromArray = function() {
 | 
			
		||||
 | 
			
		||||
// we have 2 solutions here, an easier one and a more advanced one.
 | 
			
		||||
// The easiest way to get an array of the rest of the arguments that are passed to a function
 | 
			
		||||
// is using the rest operator. If this is unfamiliar to you look it up!
 | 
			
		||||
const removeFromArray = function (array, ...args) {
 | 
			
		||||
  // create a new empty array
 | 
			
		||||
  const newArray = [];
 | 
			
		||||
  // use forEach to go through the array
 | 
			
		||||
  array.forEach((item) => {
 | 
			
		||||
    // push every element into the new array
 | 
			
		||||
    // UNLESS it is included in the function arguments
 | 
			
		||||
    // so we create a new array with every item, except those that should be removed
 | 
			
		||||
    if (!args.includes(item)) {
 | 
			
		||||
      newArray.push(item);
 | 
			
		||||
    }
 | 
			
		||||
  });
 | 
			
		||||
  // and return that array
 | 
			
		||||
  return newArray;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// A simpler, but more advanced way to do it is to use the 'filter' function,
 | 
			
		||||
// which basically does what we did with the forEach above.
 | 
			
		||||
 | 
			
		||||
// var removeFromArray = function(array, ...args) {
 | 
			
		||||
//   return array.filter(val => !args.includes(val))
 | 
			
		||||
// }
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
module.exports = removeFromArray;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,10 @@
 | 
			
		|||
const repeatString = function() {
 | 
			
		||||
 | 
			
		||||
const repeatString = function(word, times) {
 | 
			
		||||
  if (times < 0) return 'ERROR';
 | 
			
		||||
  let string = '';
 | 
			
		||||
  for (let i = 0; i < times; i++) {
 | 
			
		||||
    string += word;
 | 
			
		||||
  }
 | 
			
		||||
  return string;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = repeatString;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
const reverseString = function() {
 | 
			
		||||
 | 
			
		||||
const reverseString = function(string) {
 | 
			
		||||
 return string.split('').reverse().join('');
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = reverseString;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,19 @@
 | 
			
		|||
const snakeCase = function() {
 | 
			
		||||
const snakeCase = function(string) {
 | 
			
		||||
  // wtf case
 | 
			
		||||
  string = string.replace(/\.\./g, " ");
 | 
			
		||||
 | 
			
		||||
  // this splits up camelcase IF there are no spaces in the word
 | 
			
		||||
  if (string.indexOf(" ") < 0) {
 | 
			
		||||
    string = string.replace(/([A-Z])/g, " $1");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return string
 | 
			
		||||
    .trim()
 | 
			
		||||
    .toLowerCase()
 | 
			
		||||
    .replace(/[,\?\.]/g, "")
 | 
			
		||||
    .replace(/\-/g, " ")
 | 
			
		||||
    .split(" ")
 | 
			
		||||
    .join("_");
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = snakeCase;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,16 @@
 | 
			
		|||
const sumAll = function() {
 | 
			
		||||
 | 
			
		||||
const sumAll = function(min, max) {
 | 
			
		||||
  if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
 | 
			
		||||
  if (min < 0 || max < 0) return "ERROR";
 | 
			
		||||
  if (min > max) {
 | 
			
		||||
    const temp = min;
 | 
			
		||||
    min = max;
 | 
			
		||||
    max = temp;
 | 
			
		||||
  }
 | 
			
		||||
  let sum = 0;
 | 
			
		||||
  for (let i = min; i < max + 1; i++) {
 | 
			
		||||
    sum += i;
 | 
			
		||||
  }
 | 
			
		||||
  return sum;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = sumAll;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,6 +13,9 @@ describe('sumAll', () => {
 | 
			
		|||
  test.skip('returns ERROR with negative numbers', () => {
 | 
			
		||||
    expect(sumAll(-10, 4)).toEqual('ERROR');
 | 
			
		||||
  });
 | 
			
		||||
  test.skip('returns ERROR with non-integer parameters', () => {
 | 
			
		||||
    expect(sumAll(2.5, 4)).toEqual('ERROR');
 | 
			
		||||
  });
 | 
			
		||||
  test.skip('returns ERROR with non-number parameters', () => {
 | 
			
		||||
    expect(sumAll(10, "90")).toEqual('ERROR');
 | 
			
		||||
  });
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,12 +2,12 @@
 | 
			
		|||
 | 
			
		||||
Write two functions that convert temperatures from Fahrenheit to Celsius, and vice versa:
 | 
			
		||||
```
 | 
			
		||||
ftoc(32) // fahrenheit to celsius, should return 0
 | 
			
		||||
convertToCelsius(32) // fahrenheit to celsius, should return 0
 | 
			
		||||
 | 
			
		||||
ctof(0) // celsius to fahrenheit, should return 32
 | 
			
		||||
convertToFahrenheit(0) // celsius to fahrenheit, should return 32
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Because we are human, we want the result temperature to be rounded to one decimal place: i.e., `ftoc(100)` should return `37.8` and not `37.77777777777778`.
 | 
			
		||||
Because we are human, we want the result temperature to be rounded to one decimal place: i.e., `convertToCelsius(100)` should return `37.8` and not `37.77777777777778`.
 | 
			
		||||
 | 
			
		||||
This exercise asks you to create more than one function so the `module.exports` section of the spec file looks a little different this time.  Nothing to worry about, we're just packaging both functions into a single object to be exported.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,12 +1,13 @@
 | 
			
		|||
const ftoc = function() {
 | 
			
		||||
 | 
			
		||||
const convertToCelsius = function(fahrenheit) {
 | 
			
		||||
  return Math.round((fahrenheit - 32) * (5/9) * 10) / 10;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const ctof = function() {
 | 
			
		||||
 | 
			
		||||
const convertToFahrenheit = function(celsius) {
 | 
			
		||||
  return Math.round(((celsius * 9/5) + 32) * 10) / 10;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  ftoc,
 | 
			
		||||
  ctof
 | 
			
		||||
  convertToCelsius,
 | 
			
		||||
  convertToFahrenheit
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,25 +1,25 @@
 | 
			
		|||
const {ftoc, ctof} = require('./tempConversion')
 | 
			
		||||
const {convertToCelsius, convertToFahrenheit} = require('./tempConversion')
 | 
			
		||||
 | 
			
		||||
describe('ftoc', () => {
 | 
			
		||||
describe('convertToCelsius', () => {
 | 
			
		||||
  test('works', () => {
 | 
			
		||||
    expect(ftoc(32)).toEqual(0);
 | 
			
		||||
    expect(convertToCelsius(32)).toEqual(0);
 | 
			
		||||
  });
 | 
			
		||||
  test.skip('rounds to 1 decimal', () => {
 | 
			
		||||
    expect(ftoc(100)).toEqual(37.8);
 | 
			
		||||
    expect(convertToCelsius(100)).toEqual(37.8);
 | 
			
		||||
  });
 | 
			
		||||
  test.skip('works with negatives', () => {
 | 
			
		||||
    expect(ftoc(-100)).toEqual(-73.3);
 | 
			
		||||
    expect(convertToCelsius(-100)).toEqual(-73.3);
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
describe('ctof', () => {
 | 
			
		||||
describe('convertToFahrenheit', () => {
 | 
			
		||||
  test.skip('works', () => {
 | 
			
		||||
    expect(ctof(0)).toEqual(32);
 | 
			
		||||
    expect(convertToFahrenheit(0)).toEqual(32);
 | 
			
		||||
  });
 | 
			
		||||
  test.skip('rounds to 1 decimal', () => {
 | 
			
		||||
    expect(ctof(73.2)).toEqual(163.8);
 | 
			
		||||
    expect(convertToFahrenheit(73.2)).toEqual(163.8);
 | 
			
		||||
  });
 | 
			
		||||
  test.skip('works with negatives', () => {
 | 
			
		||||
    expect(ctof(-10)).toEqual(14);
 | 
			
		||||
    expect(convertToFahrenheit(-10)).toEqual(14);
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue