Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created February 16, 2022 02:47
Show Gist options
  • Save natafaye/b7b6b80ca1ca97183476517d22c467b0 to your computer and use it in GitHub Desktop.
Save natafaye/b7b6b80ca1ca97183476517d22c467b0 to your computer and use it in GitHub Desktop.
// Here's another solution I made previously using some fancy reduce stuff to make me look cool
// https://www.codewars.com/kata/576bb71bbbcf0951d5000044/train/javascript
function countPositivesSumNegatives(input) {
if(!input || input.length === 0) return [];
return input.reduce(
(result, num) => (num > 0) ? [++result[0], result[1]] : [result[0], result[1] + num],
[0, 0]
)
}
// https://www.codewars.com/kata/576bb71bbbcf0951d5000044/train/javascript
function countPositivesSumNegatives(input) {
// Handle empty or null inputs
if(input === null || input.length === 0)
return [];
// We could save these in variables, or we could just put them right on lines 13 and 15
// const positives = input.filter(number => number > 0);
// const negatives = input.filter(number => number < 0);
// count all the positives
const positiveCount = input.filter(number => number > 0).length;
// sum all the negatives
const negativeSum = input.filter(number => number < 0).reduce((total, number) => total + number, 0);
return [positiveCount, negativeSum];
}
// https://www.codewars.com/kata/52fba66badcd10859f00097e/train/javascript
const VOWELS = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
function disemvowel(str) {
return str.split('').filter(character => !VOWELS.includes(character)).join('');
}
// We could write isVowel like this:
//const isVowel = (character) => VOWELS.includes(character.toLowerCase());
// Or if brevity is more important than simplicity, like this:
//const isVowel = (character) => ["a", "e", "i", "o", "u"].includes(character.toLowerCase());
// We could solve it with a for loop
// function disemvowel(str) {
// let result = "";
// for(let i = 0; i < str.length; i++) {
// if( !isVowel( str[i] ) ) {
// result += str[i];
// }
// }
// return result;
// }
// Or with array methods
// function disemvowel(str) {
// const characters = str.split('');
// const result = characters.filter(character => !isVowel(character))
// return result.join('');
// }
// And simplify it down
// function disemvowel(str) {
// return str.split('').filter(character => !isVowel(character)).join('');
// }
// https://www.codewars.com/kata/5d65fbdfb96e1800282b5ee0/train/javascript
function wrap(height, width, length){
const dimensions = [height, width, length].sort((a, b) => a - b)
return (2 * dimensions[2]) + (2 * dimensions[1]) + (4 * dimensions[0]) + 20;
}
// Some of our thought process, I often might write this on a piece of paper as I thought it through
// 2 x biggest
// 2 x middle
// 4 x smallest
// 20
// A less compact but maybe more readable solution?
// function wrap(height, width, length){
// const dimensions = [height, width, length].sort((a, b) => a - b)
// const biggest = dimensions[2];
// const middle = dimensions[1];
// const smallest = dimensions[0];
// return (2 * biggest) + (2 * middle) + (4 * smallest) + 20;
// }
// A definitely overcomplicated, but let us play around with array methods solution :)
// function wrap(height, width, length){
// const dimensions = [height, width, length].sort((a, b) => a - b)
// dimensions.push(dimensions[0])
// return dimensions.map(number => number * 2).reduce((total, number) => total + number) + 20
// }
// An example of how .map() and .reduce() together can be really useful!
// const cartItems = [
// {
// name: "Toilet paper",
// price: 200
// },
// {
// name: "Paper towels",
// price: 300
// }
// ]
// const total = cartItems.map(item => item.price).reduce((total, number) => total + number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment