Easy Coderbyte challenges 3 & 4
Aug. 11th, 2015 01:29 pmRemembering these from last week: Challenge 3 was another I'd done before, "find the longest word in the sentence", and I got a few more refinements in my answer: I used a regular expression to create the first array; and (having discussed it with Ben) I used an empty string as the initializing value. The previous stab at this Reduce, where I was trying to initialize with 0, was comparing apples and oranges in the form of numbers and strings.
Challenge 4 was ...a journey. ( Read more... )
function LongestWord(sen) {
var arr = sen.split(/(\w+)/g);
var foo = arr.reduce(function(prev, curr){
if(prev.length < curr.length){
return curr;
} else {return prev;}
}, '');
return foo;
} Looking over other answers, I think reduce is by far the most elegant path. Wooo! Arrays FTW!Challenge 4 was ...a journey. ( Read more... )