Skip to content

Instantly share code, notes, and snippets.

@srkama
srkama / oddsumfib.js
Created March 3, 2016 12:32
Sum All Odd Fibonacci Numbers - freecodecamp
function sumFibs(num) {
f1=1;
f2=1;
oddNumberSums = f1 + f2;
f3=f1+f2;
while (f3<=num) {
if ((f3%2) > 0) {
oddNumberSums +=f3;
}
f1=f2;
@srkama
srkama / spinaltapcase.js
Created March 3, 2016 12:17
freecodecamp problem
function spinalCase(str) {
newString=str[0];
for (i=1;i<str.length;i++) {
console.log(str[i]);
if (str[i] == ' ' || str[i] == '_') {
if (newString[newString.length-1]!='-') {
newString+="-";
}
} else if(str[i].toUpperCase()==str[i] && str[i]!=="-") {
if (newString[newString.length-1]!='-') {
@srkama
srkama / palindromes.js
Created February 12, 2016 11:44
simple program to check string is palindrome or not
//Write your code below this line.
function reverse(s){
return s.split("").reverse().join("");
}
for(var i of my_array) {
if (i == reverse(i))
{
console.log(i);
}
}
@srkama
srkama / javascript_arrow.js
Created February 12, 2016 03:47
javascript arrow function
// write the correct arrow function here
var my_function = function(some_array) {
return some_array.map(v => (v % 2 == 0 ? v + 1 : v - 1));
}
@srkama
srkama / findDay.js
Created February 11, 2016 16:30
function to get the day as string for given date in string, expexted format is MM/DD/YYYY
function findDay(myDate) {
var dayStrArray = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
var pattern = /(\d{2})\/(\d{2})\/(\d{4})/;
var dt = new Date(myDate.replace(pattern,'$3-$1-$2'));
console.log(dayStrArray[dt.getDay()]);
}
@srkama
srkama / htmlEntityReplate.js
Created February 9, 2016 10:51
a global replace using regex in javascript
function convert(str) {
// &colon;&rpar;
htmlEntities = [
['&', '&amp;'],
['<','&lt;'],
['>', '&gt;'],
['\'','&apos;'],
['"','&quot;'],
];
myRegEx = /[^a-z\d]/i;
@srkama
srkama / unique_elements_array
Created February 2, 2016 12:26
unique elements in an array
function unite(arr1, arr2, arr3) {
return Array.prototype.slice.call(arguments).reduce(function(a,b) {
for(i=0;i<b.length;i++) {
if(a.indexOf(b[i])==-1) {
a.push(b[i])
}
}
return a;
});
}
function fearNotLetter(str) {
for(i=0;i<str.length-1;i++) {
if ((str.charCodeAt(i+1)-str.charCodeAt(i)) > 1) {
return String.fromCharCode(str.charCodeAt(i)+1);
}
}
}
fearNotLetter("abce");
pairs = [["A", "T"],["C","G"]]
function pair(str) {
tempArray = [];
for (j=0;j<str.length;j++){
for (i=0;i<pairs.length;i++) {
indexValue = pairs[i].indexOf(str.charAt(j));
if(indexValue==0) {
tempArray.push(pairs[i].slice());
} else if (indexValue == 1) {
pairs[i].reverse();
import json
def object_to_dict(obj):
"""
Method to get dict of object properties. This method identified object properties and returns
a dict of properties with key, value as property name and property value respectively
:param : object
:return: obj_dict : dict
"""
obj_dict = {}
print vars(obj.__class__)