Skip to content

Instantly share code, notes, and snippets.

@Vampeyer
Created August 26, 2024 22:13
Show Gist options
  • Save Vampeyer/6ca32ec524f8c27e93bd6dc0779ae813 to your computer and use it in GitHub Desktop.
Save Vampeyer/6ca32ec524f8c27e93bd6dc0779ae813 to your computer and use it in GitHub Desktop.
// For this kata, you'll be adding only the numbers in the array which match the given condition.
// Input
// const conditionalSum = function(values, condition) {
// // Your code here
// };
// console.log(conditionalSum([1, 2, 3, 4, 5], "even"));
// console.log(conditionalSum([1, 2, 3, 4, 5], "odd"));
// console.log(conditionalSum([13, 88, 12, 44, 99], "even"));
// console.log(conditionalSum([], "odd"));
// Expected Output
// 6
// 9
// 144
// 0
// Instruction
// Create a function named conditionalSum that will be given an array of numbers
// and a
// condition, in this case odd or even. Given this condition, add up only the
// values which
// match that condition. If no values match the condition, return 0.
let values = [1, 2, 3, 4, 5 , 99 ,22];
let condition = 'even';
let condition2 = 'odd';
let evenSum = 0
let oddSum = 0
function conditionalSums(values, condition){
// Your code here
let i = 0;
while( i < values.length){
let theValue = values[i]
if(theValue % 2 === 0){
// console.log(theValue , ' even valueeee')
evenSum += theValue
}
if(theValue % 2 !== 0){
// console.log(theValue , ' odd valueeee')
oddSum += theValue
console.log(theValue , " Oddds ? ")
}
i++
}
if(condition === 'even'){
return evenSum + ' This is the even sum '
}
if (condition === 'odd'){
return oddSum + ' This is the odd sum '
}
}
console.log(conditionalSums(values, condition))
// console.log(conditionalSum([1, 2, 3, 4, 5], "even"));
// console.log(conditionalSum([1, 2, 3, 4, 5], "odd"));
// console.log(conditionalSum([13, 88, 12, 44, 99], "even"));
// console.log(conditionalSum([], "odd"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment