-
Notifications
You must be signed in to change notification settings - Fork 12
JavaScript functions #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* convert */ | ||
| function convert(obj){ | ||
| var objKeys = Object.keys(obj); | ||
| const res = objKeys.map(function(value){ | ||
| return [value, obj[value]]; | ||
| }) | ||
| console.log(res); | ||
| } | ||
| obj1 = {name: 'Jeremy', age: 24, role: 'Software Engineer'}; | ||
| console.log(convert(obj1)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* countDown */ | ||
| function countDown(k) | ||
| { | ||
| var i = k; | ||
| const timerId = setInterval(function(){ | ||
| if(i == 0) | ||
| clearInterval(timerId); | ||
| console.log(i); i--; | ||
| }, 1000) | ||
| } | ||
| countDown(3); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* largest */ | ||
| function largest() | ||
| { | ||
| var tmp = arguments[0]; | ||
| for(var j = 0; j < arguments.length; ++j) | ||
| { | ||
| if(tmp < arguments[j]) | ||
| { | ||
| tmp = arguments[j]; | ||
| } | ||
| } | ||
| return tmp; | ||
| } | ||
| console.log(largest(2, -5, 100, 3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* myBind */ | ||
| Function.prototype.myBind = function(context){ | ||
| var myArguments = Array.prototype.slice.call(arguments, 1), | ||
| mybound = this; | ||
| return function(){ | ||
| var myBoundArguments = Array.prototype.slice.call(arguments); | ||
| return mybound.apply(context, myBoundArguments) | ||
| } | ||
| } | ||
|
|
||
| function addPropToNumber(number) { return this.prop + number; } | ||
| var bound = addPropToNumber.myBind({ prop: 9 }); | ||
| bound(1) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* reverseString */ | ||
| function reverseString(str){ | ||
| var stringArr = str.split(' '); | ||
| stringArr = stringArr.map(function(value){ | ||
| return value.split('').reverse().join(''); | ||
| }) | ||
| stringArr = stringArr.join(' '); | ||
| console.log(stringArr); | ||
| } | ||
| console.log(reverseString(" A fun little challenge! ")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* splitAndMerge */ | ||
| function splitAndMerge(str, sp){ | ||
| let wrds = str.split(' '); //Dividing sentence into words | ||
| const chars = []; | ||
| for(let i = 0; i < wrds.length; ++i) | ||
| chars.push(wrds[i].split('').join(sp)); //Dividing words into chars | ||
| chars = chars.join(' '); | ||
| console.log(chars); | ||
| } | ||
| console.log(splitAndMerge("Hello World!", ",")); | ||
| console.log(splitAndMerge("My name is John"," ")); | ||
| /* convert */ | ||
| function convert(obj){ | ||
| let objKeys = Object.keys(obj); | ||
| const res = objKeys.map(function(value){ | ||
| return [value, obj[value]]; | ||
| }) | ||
| console.log(res); | ||
| } | ||
| obj1 = {name: 'Jeremy', age: 24, role: 'Software Engineer'}; | ||
| console.log(convert(obj1)); | ||
| /* toCamelCase */ | ||
| function toCamelCase(str){ | ||
| str = str.split(/[-|_]/g); | ||
| console.log(str); | ||
| for(let i = 1; i < str.length; ++i){ | ||
| str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); | ||
| } | ||
| return str.join(''); | ||
| } | ||
| console.log(toCamelCase("the-stealth-warrior")); | ||
| console.log(toCamelCase('The_Stealth_Warrior')); | ||
| /* reverseString */ | ||
| function reverseString(str){ | ||
| str = str.split(' '); | ||
| str = str.map(function(value){ | ||
| return value.split('').reverse().join(''); | ||
| }) | ||
| str = str.join(' '); | ||
| console.log(str); | ||
| } | ||
| console.log(reverseString(" A fun little challenge! ")); | ||
| /* stringExpansion */ | ||
| /* largest */ | ||
| function largest(arr) | ||
| { | ||
| let numbers = []; | ||
| for(let i = 0; i < arguments.length; ++i) | ||
| { | ||
| numbers[i] = arguments[i]; | ||
| } | ||
| let tmp = numbers[0]; | ||
| for(let j = 0; j < numbers.length; ++j) | ||
| { | ||
| if(tmp < numbers[j]) | ||
| tmp = numbers[j]; | ||
| } | ||
| return tmp; | ||
| } | ||
| console.log(largest(2, -5, 100, 3)); | ||
| /* smallest */ | ||
| function smallest(arr) | ||
| { | ||
| let numbers = []; | ||
| for(let i = 0; i < arguments.length; ++i) | ||
| numbers[i] = arguments[i]; | ||
| let tmp = numbers[0]; | ||
| for(let j = 0; j < numbers.length; ++j) | ||
| { | ||
| if(tmp > numbers[j]) | ||
| tmp = numbers[j]; | ||
| } | ||
| return tmp; | ||
| } | ||
| console.log(smallest(2, -5, 100, 3)); | ||
| /* transform */ | ||
| function transform(arr){ | ||
| return arr.map(function(value){ | ||
| return function(){ | ||
| return value; | ||
| } | ||
| }) | ||
| } | ||
| const baseArray = [10, 20, 30, 40, 50]; | ||
| const newArray = transform(baseArray); | ||
| newArray[3](); | ||
| newArray[4](); | ||
| /* sum */ | ||
| function sum() | ||
| { | ||
| return arguments.length === 0? 0 : arguments[0] + sum.apply(0, Array.prototype.slice.call(arguments, 1)); | ||
| } | ||
| console.log(sum(2, -5, 100, 3)); | ||
| /* countDown */ | ||
| function countDown(k) | ||
| { | ||
| let i = k; | ||
| let timerId = setInterval(function(){ | ||
| if(i == 0) | ||
| clearInterval(timerId); | ||
| console.log(i); i--; | ||
| }, 1000) | ||
| } | ||
| countDown(3); | ||
| /* myBind */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* smallest */ | ||
| function smallest() | ||
| { | ||
| var tmp = arguments[0]; | ||
| for(var j = 0; j < arguments.length; ++j) | ||
| { | ||
| if(tmp > arguments[j]) | ||
| { | ||
| tmp = arguments[j]; | ||
| } | ||
| } | ||
| return tmp; | ||
| } | ||
| console.log(smallest(2, -5, 100, 3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* splitAndMerge */ | ||
| function splitAndMerge(str, sp){ | ||
| var wrds = str.split(' '); | ||
| var modifiedWrds = []; | ||
| for(let i = 0; i < wrds.length; ++i) | ||
| { | ||
| modifiedWrds.push(wrds[i].split('').join(sp)); | ||
| } | ||
| modifiedWrds = modifiedWrds.join(' '); | ||
| console.log(modifiedWrds); | ||
| } | ||
| console.log(splitAndMerge("Hello World!", ",")); | ||
| console.log(splitAndMerge("My name is John"," ")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* stringExpansion */ | ||
| function stringExpansion(str){ | ||
| return str.match(/\d?[A-Za-z]/ig).reduce(function(acc, currItem){ | ||
| if(!parseInt(currItem)) | ||
| { | ||
| acc.push(currItem); | ||
| } | ||
| acc.push(currItem[currItem.length-1].repeat(parseInt(currItem))); | ||
| return acc; | ||
| }, []).join(''); | ||
| } | ||
|
|
||
| console.log(stringExpansion('3D2a5d2f')); | ||
| console.log(stringExpansion('3d332f2a')); | ||
| console.log(stringExpansion('abcde')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /* sum */ | ||
| function sum() | ||
| { | ||
| return arguments.length === 0? 0 : arguments[0] + sum.apply(0, Array.prototype.slice.call(arguments, 1)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I'm ok with that solution, but maybe better to use here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, ignore this comment - I didn't see that recursion is the requirement |
||
| } | ||
| console.log(sum(2, -5, 100, 3)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* toCamelCase */ | ||
| function toCamelCase(str){ | ||
| var stringArr = str.split(/[-|_]/g); | ||
| console.log(stringArr); | ||
| for(let i = 1; i < stringArr.length; ++i){ | ||
| stringArr[i] = stringArr[i].charAt(0).toUpperCase() + stringArr[i].slice(1); | ||
| } | ||
| return stringArr.join(''); | ||
| } | ||
| console.log(toCamelCase("the-stealth-warrior")); | ||
| console.log(toCamelCase('The_Stealth_Warrior')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* transform */ | ||
| function transform(arr){ | ||
| return arr.map(function(value){ | ||
| return function(){ | ||
| return value; | ||
| } | ||
| }) | ||
| } | ||
| const baseArray = [10, 20, 30, 40, 50]; | ||
| const newArray = transform(baseArray); | ||
| newArray[3](); | ||
| newArray[4](); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use
for,ifwithout brackets - it decreases readability of your code