Skip to content

Instantly share code, notes, and snippets.

@Go7hic
Created May 20, 2014 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Go7hic/05c493f2fde27a6a6699 to your computer and use it in GitHub Desktop.
Save Go7hic/05c493f2fde27a6a6699 to your computer and use it in GitHub Desktop.
// Sort Numbers
function solution(nums){
return (nums || []).sort(function(a, b){
return a - b
});
}
// -------------------------------------------------------------------------------------------------------------------------
// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
// The sum of these multiples is 23.
// Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
function solution(number){
var sum = 0;
for(var i = 1;i< number; i++){
if(i % 3 == 0 || i % 5 == 0){
sum += i
}
}
return sum;
}
// -------------------------------------------------------------------------------------------------------------------------
// For this exercise you should create a JavaScript class like object called "Animal" that
// takes in "name" and "type" arguments. It should have a toString method that returns a human
// readable string indicating the argument information passed in.
// create your Animal class like object here
var Animal = function (name, type) {
this.type = type;
this.name = name;
};
Animal.prototype.toString = function () {
return this.name + ' is a ' + this.type;
};
// -------------------------------------------------------------------------------------------------------------------------
// Complete the solution so that it takes the object (JavaScript/CoffeeScript), or hash (ruby) passed in and
// generates a human readable string from its key/value pairs.
// The format should be "KEY = VALUE". Each key/value pair should be separated by a comma except for the last pair.
function solution(pairs) {
return Object.keys(pairs)
.map(function(k) { return k + ' = ' + pairs[k] })
.join(',');
}
// -------------------------------------------------------------------------------------------------------------------------
// Complete the solution so that it returns true if the first argument(string)
// passed in ends with the 2nd argument (also a string).
function solution(str, ending) {
return str.substring(str.length - ending.length) == ending;
}
// -------------------------------------------------------------------------------------------------------------------------
// Just a simple sorting usage. Create a function that returns the elements of the input-array in a sorted manner.
sortme = function( names ){
return names.sort()
}
// -------------------------------------------------------------------------------------------------------------------------
// Complete the keysAndValues function so that it takes in an object and returns the keys and values as separate arrays.
function keysAndValues(data){
var keys = Object.getOwnPropertyNames(data),
vals = keys.map(function (key) { return data[key]; });
return [keys, vals];
}
// -------------------------------------------------------------------------------------------------------------------------
// Complete the solution so that it reverses the string value passed into it.
function solution(str){
return str.split('').reverse().join('');
}
// -------------------------------------------------------------------------------------------------------------------------
// JavaScript Array's support a filter function (starting in JavaScript 1.6).
// Use the filter functionality to complete the function given.
function getEvenNumbers(numbersArray){
return numbersArray.filter(function(n){
return n % 2 == 0;
});
}
// -------------------------------------------------------------------------------------------------------------------------
// In this kata we want to test, if you are able to extend the functionality of standard prototypes.
// Extend the Array prototype in order to support the methods square(), cube(), average(), sum(), even() and odd().
// Explanation:
// square() must return a copy of the array, containing all values squared, the original array must not be changed
// cube() must return a copy of the array, containing all values cubed, the original array must not be changed
// average() must return the average of all array values, average() on an empty array must return NaN
// sum() must return the sum of all array values
// even() must return an array of all even numbers, the original array must not be changed
// odd() must return an array of all odd numbers, the original array must not be changed
Array.prototype.square = function () { return this.map(function(n) { return n*n; }); }
Array.prototype.cube = function () { return this.map(function(n) { return n*n*n; }); }
Array.prototype.average = function () { return this.sum() / this.length; }
Array.prototype.sum = function () { return this.reduce(function(a, b) { return a + b; }, 0); }
Array.prototype.even = function () { return this.filter(function(item) { return 0 == item % 2; }); }
Array.prototype.odd = function () { return this.filter(function(item) { return 0 != item % 2; }); }
// -------------------------------------------------------------------------------------------------------------------------
// Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument
// (also a string).
function solution(str, ending){
return str.substring(str.length - ending.length) == ending;
}
// -------------------------------------------------------------------------------------------------------------------------
// Complete the function so that it returns the number of seconds that have elapsed between the start and end times given.
function elapsedSeconds(startDate, endDate){
return (endDate - startDate) / 1000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment