Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sum by Type solution in Uncategorized category for Sum by Type by Elena_Korljukova
import assert from "assert";
function sumByTypes(values: Array): [string, number] {
let num = 0;
let s = '';
for (let i of values){
if (typeof(i) == 'string') s += i;
else num += i;
}
return [s,num];
}
console.log('Example:');
console.log(sumByTypes([]));
// These "asserts" are used for self-checking
assert.deepEqual(sumByTypes([]), ['', 0]);
assert.deepEqual(sumByTypes([1, 2, 3]), ['', 6]);
assert.deepEqual(sumByTypes(['1', 2, 3]), ['1', 5]);
assert.deepEqual(sumByTypes(['1', '2', 3]), ['12', 3]);
assert.deepEqual(sumByTypes(['1', '2', '3']), ['123', 0]);
assert.deepEqual(sumByTypes(['size', 12, 'in', 45, 0]), ['sizein', 57]);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
June 27, 2020
Comments: