Skip to content

Instantly share code, notes, and snippets.

View SerhiiLihus's full-sized avatar
🦊

Serhii SerhiiLihus

🦊
View GitHub Profile
@SerhiiLihus
SerhiiLihus / bonfire-make-a-person(Upper Intermediate Algorithm).js
Created January 15, 2016 12:33
Fill in the object constructor with the methods specified in the tests. Those methods are getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(last), and setFullName(firstAndLast). These methods must be the only available means for interacting with the object.
// Bonfire: Make a Person(Upper Intermediate Algorithm)
// Author: @serhiilihus
// http://www.freecodecamp.com/challenges/bonfire-make-a-person#?solution=var%20Person%20%3D%20function(firstAndLast)%20%7B%0A%20%20var%20fullName%20%3D%20firstAndLast%3B%0A%20%20this.getFullName%20%3D%20function()%20%7B%0A%20%20%20%20return%20fullName%3B%0A%20%20%7D%3B%0A%20%20this.getFirstName%20%3D%20function()%20%7B%0A%20%20%20%20return%20fullName.split(%22%20%22)%5B0%5D%3B%0A%20%20%7D%3B%0A%20%20this.getLastName%20%3D%20function()%20%7B%0A%20%20%20%20return%20fullName.split(%22%20%22)%5B1%5D%3B%0A%20%20%7D%3B%0A%20%20this.setFirstName%20%3D%20function(first)%20%7B%0A%20%20%20%20this.setFullName(first%20%2B%20%22%20%22%20%2B%20fullName.split(%22%20%22)%5B1%5D)%3B%0A%20%20%7D%3B%0A%20%20this.setLastName%20%3D%20function(last)%20%7B%0A%20%20%20%20this.setFullName(fullName.split(%22%20%22)%5B0%5D%20%2B%20%22%20%22%20%2B%20last)%3B%0A%20%20%7D%3B%0A%20%20this.setFullName%20%3D%20function(firstAndLast)%20%7B%0A%20%20%20%20fullName%
@SerhiiLihus
SerhiiLihus / bonfire-map-the-debris(Upper Intermediate Algorithm).js
Created January 15, 2016 11:44
Return a new array that transforms the element's average altitude into their orbital periods.
// Bonfire: Map the Debris(Upper Intermediate Algorithm)
// Author: @serhiilihus
// http://www.freecodecamp.com/challenges/bonfire-map-the-debris#?solution=function%20orbitalPeriod(arr)%20%7B%0A%20%20var%20GM%20%3D%20398600.4418%3B%0A%20%20var%20earthRadius%20%3D%206367.4447%3B%0A%20%20var%20newArr%20%3D%20%5B%5D%2C%20period%3B%0A%20%20arr.forEach(function(value)%7B%0A%20%20%20period%20%3D%20Math.round(2*Math.PI*Math.sqrt(Math.pow(value.avgAlt%20%2B%20earthRadius%2C3)%2FGM))%3B%0A%20%20%20newArr.push(%7Bname%20%3A%20value.name%2C%20orbitalPeriod%20%3A%20period%7D)%3B%0A%20%20%7D)%3B%0A%20%20return%20newArr%3B%0A%7D%0A%0AorbitalPeriod(%5B%7Bname%20%3A%20%22sputnik%22%2C%20avgAlt%20%3A%2035873.5553%7D%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
var newArr = [], period;
arr.forEach(function(value){
@SerhiiLihus
SerhiiLihus / bonfire-caesars-cipher.js
Created January 5, 2016 09:08
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
// Bonfire: Caesars Cipher
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-caesars-cipher#?solution=function%20rot13(encodedStr)%20%7B%0A%20%20var%20codeArr%20%3D%20encodedStr.split(%22%22)%3B%20%20%2F%2F%20String%20to%20Array%0A%20%20var%20decodedArr%20%3D%20%5B%5D%2C%20newletterCode%3B%20%2F%2F%20Your%20Result%20goes%20here%0A%20%20%2F%2F%20Only%20change%20code%20below%20this%20line%0A%20%20codeArr.forEach(function(letter)%7B%0A%20%20%20%20%20%20%20newletterCode%20%3D%20(letter.charCodeAt()%20%3C%2065%20%7C%7C%20letter.charCodeAt()%20%3E%2090)%3F%20letter.charCodeAt()%20%3A%20(letter.charCodeAt()%20%2B%2013%20%3E%2090%20)%3F%20letter.charCodeAt()%20-%2013%20%3A%20letter.charCodeAt()%20%2B%2013%3B%0A%20%20%20%20%20%20%20decodedArr.push(String.fromCharCode(newletterCode))%3B%0A%20%20%7D)%3B%0A%20%20%2F%2F%20Only%20change%20code%20above%20this%20line%0A%20%20return%20decodedArr.join(%22%22)%3B%20%2F%2F%20Array%20to%20String%0A%7D%0A%0A%2F%2F%20Change%20the%20inputs%20below%
@SerhiiLihus
SerhiiLihus / bonfire-Where-art-thou(Intermediate).js
Created December 25, 2015 07:55
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument).
// Bonfire: Where art thou
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-art-thou#?solution=function%20where(collection%2C%20source)%20%7B%0A%20%20var%20arr%20%3D%20%5B%5D%2C%20count%3B%0A%20%20var%20sourceObject%20%3D%20Object.keys(source)%3B%0A%20%20for%20(%20var%20i%20%3D%200%3B%20i%20%3C%20collection.length%20%3B%20i%2B%2B)%20%7B%0A%20%20%09count%20%3D%200%3B%0A%20%20%20%20for%20(%20var%20j%20%3D%200%3B%20j%20%3C%20sourceObject.length%20%3B%20j%2B%2B)%0A%20%20%20%20%20%20if%20(collection%5Bi%5D.hasOwnProperty(sourceObject%5Bj%5D)%20%26%26%20(collection%5Bi%5D%5BsourceObject%5Bj%5D%5D%20%3D%3D%3D%20source%5BsourceObject%5Bj%5D%5D)%20)%20count%2B%2B%3B%20%0A%20%20%20%20%20if%20(count%20%3D%3D%3D%20sourceObject.length)%20arr.push(collection%5Bi%5D)%3B%0A%20%20%7D%0A%20%20return%20arr%3B%0A%7D%0A%0Awhere(%5B%7B%20first%3A%20%22Romeo%22%2C%20last%3A%20%22Montague%22%20%7D%2C%20%7B%20first%3A%20%22Mercutio%22%2C%20last%3A%20null%20%7D%2C%20%7B%20first%3A%20%22Tybalt%
@SerhiiLihus
SerhiiLihus / bonfire-seek-and-destroy.js
Created December 13, 2015 07:07
Remove all elements from the initial array that are of the same value as these arguments.
// Bonfire: Seek and Destroy
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-seek-and-destroy#?solution=function%20destroyer%28arr%29%20{%0A%20%20%0A%20%20for%20%28%20var%20i%20%3D%201%20%2C%20num%3B%20i%20%3C%20arguments.length%20%3B%20i%2B%2B%20%29%20{%0A%20%20%20%20num%20%3D%20arguments[i]%3B%0A%20%20%09arr%20%3D%20arr.filter%28filtering%29%3B%0A%20%20}%0A%20%20%0A%20%20function%20filtering%20%28%20value%20%29%20{%0A%20%20%20%20return%20value%20!%3D%3D%20num%3B%0A%20%20}%0A%20%20%0A%20%20return%20arr%3B%0A}%0A%0Adestroyer%28[1%2C%202%2C%203%2C%201%2C%202%2C%203]%2C%202%2C%203%29%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function destroyer(arr) {
for ( var i = 1 , num; i < arguments.length ; i++ ) {
num = arguments[i];
arr = arr.filter(filtering);
}
@SerhiiLihus
SerhiiLihus / bonfire-where-do-i-belong#.js
Created December 6, 2015 14:40
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.
// Bonfire: Where do I belong
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong#?solution=function%20where%28arr%2C%20num%29%20{%0A%20%20arr.sort%28function%20%28a%2Cb%29{%0A%20%20%20%20return%20a%20-%20b%3B%0A%20%20}%29%3B%0A%20%20var%20i%20%3D%200%3B%0A%20%20while%20%28%20num%20%3E%20arr[i]%29%20i%2B%2B%3B%0A%20%20return%20i%3B%0A}%0A%0Awhere%28[40%2C%2060]%2C%2050%29%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function where(arr, num) {
arr.sort(function (a,b){
return a - b;
});
var i = 0;
@SerhiiLihus
SerhiiLihus / bonfire-falsy-bouncer.js
Created December 4, 2015 13:15
Remove all falsy values from an array
// Bonfire: Falsy Bouncer
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer?solution=function%20bouncer(arr)%20%7B%0A%20%20%20return%20arr.filter(function%20(value)%20%7B%0A%20%20%20%20if%20(new%20Boolean(value).valueOf()%20%3D%3D%3D%20true)%20return%20value%3B%0A%20%20%7D)%3B%0A%7D%0A%0Abouncer(%5B7%2C%20%22ate%22%2C%20%22%22%2C%20false%2C%209%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function bouncer(arr) {
return arr.filter(function (value) {
if (new Boolean(value).valueOf() === true) return value;
});
}
@SerhiiLihus
SerhiiLihus / bonfire-mutations.js
Created December 4, 2015 12:11
Check if first word in array contain all letters from second word in array
// Bonfire: Mutations
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations?solution=function%20mutation(arr)%20%7B%0A%20%20var%20i%20%3D%200%3B%0A%20%20arr%5B0%5D%20%3D%20arr%5B0%5D.toLowerCase()%3B%0A%20%20while%20(arr%5B0%5D.indexOf(arr%5B1%5D.charAt(i).toLowerCase())%20!%3D%20-1%20%26%26%20i%20%3C%20arr%5B1%5D.length%20)%20i%2B%2B%3B%0A%20%20%0A%20%20return%20(i%20%3D%3D%3D%20arr%5B1%5D.length)%3F%20true%20%3A%20false%3B%0A%7D%0A%0Amutation(%5B%22hello%22%2C%20%22hey%22%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function mutation(arr) {
var i = 0;
arr[0] = arr[0].toLowerCase();
while (arr[0].indexOf(arr[1].charAt(i).toLowerCase()) != -1 && i < arr[1].length ) i++;
return (i === arr[1].length)? true : false;
}
// Bonfire: Slasher Flick
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-slasher-flick?solution=function%20slasher(arr%2C%20howMany)%20%7B%0A%20%20if%20(%20arr.length%20%3D%3D%3D%200%20)%20%7B%0A%20%20%20%20%20%20return%20arr%3B%0A%20%20%7D%20else%20if%20(%20howMany%20%3E%20arr.length%20)%20%7B%0A%20%20%20%20%20%20return%20%5B%5D%3B%0A%20%20%7D%20else%20arr.splice(0%2ChowMany)%3B%0A%20%20%0A%20%20return%20arr%3B%0A%7D%0A%0Aslasher(%5B1%2C%202%2C%203%5D%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function slasher(arr, howMany) {
if ( arr.length === 0 ) {
return arr;
} else if ( howMany > arr.length ) {
return [];
} else arr.splice(0,howMany);
@SerhiiLihus
SerhiiLihus / bonfire-chunky-monkey.js
Last active December 2, 2015 14:29
Chunky Monkey
// Bonfire: Chunky Monkey
// Author: @serhiilihus
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20var%20bigArr%20%3D%20%5B%5D%3B%0A%20%20var%20smallArr%3B%0A%20%20%20%20%20for%20(%20var%20i%20%3D%200%20%3B%20i%20%3C%20Math.round(arr.length%20%2F%20size)%20%3B%20i%2B%2B%20)%20%7B%0A%20%20%20%20%20%20%20%20smallArr%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20%20if%20(%20i%20*%20size%20%2B%20size%20%3E%20arr.length%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20size%20%3D%20(i%20*%20size%20%2B%20size%20)%20-%20arr.length%3B%0A%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20for%20(%20var%20j%20%3D%20i%20*%20size%20%3B%20j%20%3C%20i%20*%20size%20%2B%20size%20%3B%20j%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20smallArr.push(arr%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bigArr.push(smallArr)%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%7D%0A%20%20return%20bigArr%3B%0A%7D%0A%0Achunk(%5B%2