Dragoris9 years ago
Third Greatest
Fundamentals
Arrays
Data Types
Given an array of numbers, return the third greatest number in the array.
You will always be given an array of valid numbers with at least three values.
Ex:
thirdGreatest([4,8,1,5,3]) -> 4
function thirdGreatest(arr){
return arr.sort( (a,b) => a - b)[arr.length-3]
}
describe("Solution", function(){
it("did you return the correct number?", function(){
Test.assertEquals(thirdGreatest([4,8,1,5,3]), 4, "Better try again");
Test.assertEquals(thirdGreatest([45,38,1,0,89,6,77]), 45, "Better try again");
Test.assertEquals(thirdGreatest([878,1,1,0,46,123]), 46, "Better try again");
Test.assertEquals(thirdGreatest([8,99,0]), 0, "Better try again");
Test.assertEquals(thirdGreatest([1,1,2,3,3,4]), 3, "Better try again");
Test.assertEquals(thirdGreatest([79831,436,683,3645,2399,0,0,66]), 2399, "Better try again");
Test.assertEquals(thirdGreatest([6,33,7,41,3,388]), 33, "Better try again");
});
});
Dragoris9 years ago
Convert Minutes to Hours and Minutes
Fundamentals
Strings
Data Types
You will be given a number of minutes. Your task is to return a string that formats the number into 'hours:minutes". Make sure you always return two intigers for the minutes section of the conversion.
ie ('0:01' instead of '0:1')
Example:
minutes(90) => '1:30'
function minutes(num){
return num % 60 < 10 ? Math.floor(num/60) + ':' + '0' + (num % 60) : Math.floor(num/60) + ':' + (num % 60)
}
describe("Solution", function(){
it("Did you make the correct conversion?", function(){
Test.assertEquals(minutes(0), "0:00", "better try again");
Test.assertEquals(minutes(1), "0:01", "better try again");
Test.assertEquals(minutes(18), "0:18", "better try again");
Test.assertEquals(minutes(13267), "221:07", "better try again");
Test.assertEquals(minutes(985), "16:25", "better try again");
Test.assertEquals(minutes(351), "5:51", "better try again");
Test.assertEquals(minutes(156113), "2601:53", "better try again");
});
});
For a given number (num) add up all numbers from 0 to num.
This is similar to a factorial, but using addition.
function numSum(num){
var sum = 0;
for (var i = 1; num >= i; i++) {
sum += i
}
return sum
};
describe("Solution", function(){
it("Did you get the right number?", function(){
Test.assertEquals(numSum(5), 15, "Better try again");
Test.assertEquals(numSum(64), 2080, "Better try again");
Test.assertEquals(numSum(1648), 1358776, "Better try again");
Test.assertEquals(numSum(0), 0, "Better try again");
Test.assertEquals(numSum(-1), 0, "Better try again");
});
});
Dragoris9 years ago
Longest Word
Find the longest word in a given string.
Assume all strings will contain only letters and spaces.
function longestString(str){
return str.split(' ').sort(function(a,b) {
return b.length - a.length
})[0]
}
describe("Solution", function(){
it("did you find the longest?", function(){
Test.assertEquals(longestString('I went running around the park'), "running", "try again");
Test.assertEquals(longestString('There were a lot of barking dogs everywhere'), "everywhere", "try again");
Test.assertEquals(longestString('now for some reason my shoe smells funny'), "reason", "try again");
});
});