This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// figure out how this works and why it's important | |
// array | |
var arr =[2,3,5,5,1,99,22,44]; | |
// sort ascending | |
arr.sort(function(n1,n2){ | |
return n1 - n2; | |
}); //output: [1, 2, 3, 5, 22, 44, 99] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. declare a "named" function | |
function myNamedFunction(){ return true; } | |
// assertions | |
console.log(typeof window.myNamedFunction === "function"); // true | |
console.log(myNamedFunction.name === "myNamedFunction"); // true | |
// 2. declare anonymous function | |
var myAnonymousFunction = function(){ return true; }; | |
//assertions | |
console.log(typeof window.myAnonymousFunction === "function"); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//reverse characters in string | |
console.log('test string'.split('').reverse().join('')); // output: gnirts tset | |
// count characters in a string | |
function Char_Counts(str1) { | |
var uchars = {}; | |
str1.replace(/\S/g, function(l){ | |
uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get factorial of a given number | |
// ie. 4 = (4 * 3 * 2 * 1) = 24 | |
function factorial(x) { | |
if(x==0) { | |
return 1; | |
} | |
return x * fact(x-1); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return 2nd highest and lowest numbers | |
function Second_Greatest_Lowest(arr_num) | |
{ | |
arr_num.sort(function(x,y) | |
{ | |
return x-y; | |
}); | |
var uniqa = [arr_num[0]]; | |
var result = []; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filter words with a RegEx | |
var startsWithA = new RegExp(/^a/i); // filter by first letter A | |
var arr = ['Apple','avocado','Banana','Cherry']; | |
var filtered = arr.filter(function(item){ | |
return startsWithA.test(item); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LongestWord(sen) { | |
sen = sen.split(' '); // creates array | |
var nonAlpha = new RegExp('[^a-zA-Z\d\s:]'); | |
var filtered = sen.filter(function(item){ | |
return !nonAlpha.test(item); | |
}); | |
var sorted = filtered.sort(function(n1,n2){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getType(value) | |
{ | |
var dtypes = [Function, RegExp, Number, String, Boolean, Object], x, len; | |
if (typeof value === "object" || typeof value === "function") { | |
for (x = 0, len = dtypes.length; x < len; x++) | |
{ | |
if (value instanceof dtypes[x]) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LetterChanges(str) { | |
var letters = str.split(''); | |
for (var i = 0; i < letters.length; i++) { | |
var letter = letters[i]; | |
if(/[a-yA-Y]/.test(letter)){ | |
letters[i] = String.fromCharCode(letter.charCodeAt(letter.length-1)+1); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Setup steps via terminal: | |
# 1. create a .rb file : $ touch askwiki.rb | |
# 2. open file: $ open askwiki.rb | |
# 3. copy/paste below in file and close/save | |
# 4. run it: $ ruby askwiki.rb | |
require 'open-uri' | |
require 'json' | |
language = 'en' |
OlderNewer