diff --git a/calculateFrequency.js b/calculateFrequency.js new file mode 100644 index 0000000..c31ec68 --- /dev/null +++ b/calculateFrequency.js @@ -0,0 +1,15 @@ +function calculateFrequency(string) { + let toReturn = {}; + for (let i = 0; i < string.length; i++) { + if (string.charCodeAt(i) > 96 && string.charCodeAt(i) < 123) { + toReturn[string.charAt(i)] = 0; + } + } + for (let i = 0; i < string.length; i++) { + if (string.charCodeAt(i) > 96 && string.charCodeAt(i) < 123) { + toReturn[string.charAt(i)] = toReturn[string.charAt(i)] + 1; + } + } + return toReturn; +} + diff --git a/flatten.js b/flatten.js new file mode 100644 index 0000000..cf09839 --- /dev/null +++ b/flatten.js @@ -0,0 +1,16 @@ +function flatten(unflatObject) { + var toReturn = {}, + count = 0; + for (var i in unflatObject) { + if ((typeof unflatObject[i]) == 'object') { + var flatObject = flatten(unflatObject[i]); + for (var x in flatObject) { + toReturn[i + '.' + x] = flatObject[x]; + } + } else { + toReturn[i] = unflatObject[i]; + } + } + return toReturn; +} + diff --git a/readme.txt b/readme.txt deleted file mode 100644 index e69de29..0000000 diff --git a/secondLargest.js b/secondLargest.js new file mode 100644 index 0000000..cf63a2c --- /dev/null +++ b/secondLargest.js @@ -0,0 +1,25 @@ +function secondLargest(array) { + let v = []; + v.length = 1000; + for (let i = 0; i < v.length; i++) { + v[i] = 0; + } + len = array.length; + for (let i = 0; i < array.length; i++) { + if (v[array[i]] == 1) { + len = len - 1; + } else { + v[array[i]] = 1; + } + } + let i = 0, + count = 0; + while (count < len - 1) { + if (v[i] == 1) { + count = count + 1; + } + i++; + } + return i - 1; +} + diff --git a/unflatten.js b/unflatten.js new file mode 100644 index 0000000..ed53293 --- /dev/null +++ b/unflatten.js @@ -0,0 +1,30 @@ +function unflatten(flatObject) { + temp = {}; + for (var key in flatObject) { + array = key.split('.'); + var ret = function (obj, array, count) { + if (count < array.length - 1) { + if (typeof obj[array[count]] == 'object') { + obj[array[count]]; + ret(obj[array[count]], array, count + 1); + } + else { + obj[array[count]] = {}; + ret(obj[array[count]], array, count + 1); + } + } + else { + obj[array[count]] = flatObject[key]; + } + return obj; + } + var extend = function (obj, src) { + for (var key in src) { + if (src.hasOwnProperty(key)) obj[key] = src[key]; + } + return obj; + } + temp = extend(ret(temp, array, 0), temp); + } + return temp; +} \ No newline at end of file