JSFiddle - React, Tailwind, and code Playground
by ronilan
HTML
Tempest Hiring Question
JavaScript
/**
* Tempest Hiring Question:
*
* Yaron (Ron) Ilan <[email protected]>
* Copyright 2016
* Fabriqué au Canada : Made in Canada
*
* https://news.ycombinator.com/item?id=12851801
* http://ca.indeed.com/job/web-application-developer-remote-c10ae2d41ae4404d
**/
/**
* sumTwoLargest will returns the sum of the two largest numeric elements in the array.
* if input is not an array, or, if the array contains something other than a number it will return null.
* if input is a single element array, it will return that elements value.
*
* @param {array} arr - an array of numbers (integers or floats).
* @return {integer} result - the sume of the two largest integers in the array.
**/
function sumTwoLargest (arr) {
'use strict';
// return null for anything that can't be computed
var result = null;
if (arr.constructor === Array && !arr.some(isNaN)) {
// one element
if (arr.length === 1) {
result = arr[0];
}
// sort and get sum of first elements
if (arr.length > 1) {
arr.sort(function (a, b) {
return b - a;
});
result = arr[0] + arr[1];
}
}
return result;
}
/** Tests
* A set of test cases for sumTwoLargest.
* Note, you may use factory functions to generate test cases.
**/
var tests = [{
case: 'not array',
expected: null
}, {
case: [],
expected: null
}, {
case: [3, 'A', 8, -20, 0],
expected: null
}, {
case: [0],
expected: 0
}, {
case: [1, 2, 3],
expected: 5
}, {
case: [-1, -2, -3],
expected: -3
}, {
case: [3, 17, 8, -20, 0],
expected: 25
}, {
case: [3, 17, 8, -20, 0, 3, 17, 8, -20, 0, 3, 17, 8, -20, 0, 3, 17, 8, -20, 0],
expected: 34
}, {
case: [1, 2.2, 3.3],
expected: 5.5
}, {
case: (function () {
'use strict';
return [1, 1, 5];
}()),
expected: 6
}, {
case: (function () {
'use strict';
var arr = [];
var i = 0;
while (i <= 500000) {
arr.push(Math.floor((Math.random() * 10000) + 1));
i++;
}
return arr;
}()),
...