Sample solutions to the exercises in the "JavaScript Under Pressure" game. The game itself can be found at this URL: http://toys.usvsth3m.com/javascript-under-pressure/
A Pen by Zakir Tariverdiev on CodePen.
// ====================================================== | |
// Sample solutions to the exercises in the "JavaScript | |
// Under Pressure" game. The game itself can be found | |
// at this URL: | |
// http://toys.usvsth3m.com/javascript-under-pressure/ | |
// | |
// Felix Saparelli offered function-style variants of the | |
// solutions, which can be found at this URL: | |
// https://gist.github.com/passcod/7639695 | |
// ====================================================== | |
// ====================================================== | |
// Problem 1 | |
// ====================================================== | |
function doubleInteger(i) { | |
// i will be an integer. Double it and return it. | |
return i * 2; | |
} | |
// ====================================================== | |
// Problem 2 | |
// ====================================================== | |
function isNumberEven(i) { | |
// i will be an integer. Return true if it's even, | |
// and false if it isn't. | |
return i % 2 === 0; | |
} | |
// ====================================================== | |
// Problem 3 | |
// ====================================================== | |
function getFileExtension(i) { | |
// i will be a string, but it may not have a file extension. | |
// return the file extension (with no period) if it has one, | |
// otherwise false | |
var e = i.search(/\..+/) | |
if (e !== -1) { | |
return i.substr(e + 1); | |
} | |
else { | |
return false; | |
} | |
} | |
// Although above passes the test, | |
// this is a much more reliable solution. | |
function getFileExtension(i) { | |
var result = i.split('.'); | |
return result.length > 1 ? result.pop() : false; | |
} | |
// ====================================================== | |
// Problem 4 | |
// ====================================================== | |
function longestString(i) { | |
// i will be an array. | |
// return the longest string in the array | |
var longest = '' | |
if (Array.isArray(i)) { | |
for (var n = 0, len = i.length; n < len; ++n) { | |
if ((typeof i[n] === 'string') && | |
(i[n].length > longest.length)) { | |
longest = i[n]; | |
} | |
} | |
} | |
return longest; | |
} | |
// Alternate solution with some | |
// functional programming thrown in. | |
function longestString(i) { | |
var longest = ''; | |
if (Array.isArray(i)) { | |
var a = i.filter(function(t) { | |
return typeof t === 'string'; | |
}).forEach(function(t) { | |
if (t.length > longest.length) { | |
longest = t; | |
} | |
}); | |
return longest; | |
} | |
} | |
// ====================================================== | |
// Problem 5 | |
// ====================================================== | |
function arraySum(i) { | |
// i will be an array, containing integers, strings | |
// and/or arrays like itself. Sum all the integers you | |
// find, anywhere in the nest of arrays. | |
var sum = 0; | |
if (Array.isArray(i)) { | |
for (var n = 0, len = i.length; n < len; ++n) { | |
if (Array.isArray(i[n])) { | |
sum += arraySum(i[n]); | |
} | |
else if (typeof i[n] === 'number') { | |
sum += i[n]; | |
} | |
} | |
} | |
return sum; | |
} |
Sample solutions to the exercises in the "JavaScript Under Pressure" game. The game itself can be found at this URL: http://toys.usvsth3m.com/javascript-under-pressure/
A Pen by Zakir Tariverdiev on CodePen.