-
Notifications
You must be signed in to change notification settings - Fork 12
10 functions. Anastasiya Santalova #8
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
Open
AnastasiyaSantalova
wants to merge
7
commits into
epam-js-jun-2019:master
Choose a base branch
from
AnastasiyaSantalova:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
706dd7d
Without 5 and 10 Functions
AnastasiyaSantalova f088ea8
All 10 functions
AnastasiyaSantalova 07b8ddf
Small changes
AnastasiyaSantalova 7a5f5a8
Improved 10 function
AnastasiyaSantalova a638f10
Small changes
AnastasiyaSantalova 0afd55d
Some improvements
AnastasiyaSantalova c846f7e
Added some improvements
AnastasiyaSantalova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Functions JS</title> | ||
| </head> | ||
|
|
||
| <body> | ||
|
|
||
| <script type="text/javascript" src="hw3.js"> </script> | ||
|
|
||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,308 @@ | ||
| "use strict" | ||
|
|
||
| // FUNCTION 1: returns phrase with determined separator (via reduce) | ||
|
|
||
| function splitAndMerge(str, sp) { | ||
|
|
||
| //separate phrase into words | ||
| var arrOfWords = str.split(" "); | ||
|
|
||
| var arrOfLetters = arrOfWords.map(function(word) { | ||
|
|
||
| //separate words into letters | ||
| var letters = word.split(""); | ||
|
|
||
| //add new separator between letters | ||
| return letters.reduce(function(acc, letter) { | ||
| return acc + sp + letter; | ||
| }); | ||
| }); | ||
|
|
||
| //add separator between words | ||
| return arrOfLetters.reduce(function(acc, word) { | ||
| return acc + " " + word; | ||
| }); | ||
| } | ||
|
|
||
| console.log("FUNCTION 1 (via reduce): " + splitAndMerge("Hello World!", ",")); | ||
|
|
||
| // FUNCTION 1: returns phrase with determined separator (via join) | ||
|
|
||
| function splitAndMerge1(str, sp) { | ||
|
|
||
| //separate phrase into words | ||
| var arrOfWords = str.split(" "); | ||
|
|
||
| var arrOfLetters = arrOfWords.map(function(word) { | ||
|
|
||
| //separate words into letters | ||
| var letters = word.split(""); | ||
|
|
||
| //add new separator between letters | ||
| return letters.join(sp); | ||
| }); | ||
|
|
||
| //add separator between words | ||
| return arrOfLetters.join(" "); | ||
| } | ||
|
|
||
| console.log("FUNCTION 1 (via join): " + splitAndMerge1("My name is John", "/")); | ||
|
|
||
|
|
||
| // FUNCTION 2: convertation hash to array (via entries) | ||
|
|
||
| function convert(hash) { | ||
| return Object.entries(hash); | ||
| } | ||
|
|
||
| var obj = {name: 'Jeremy', age: 24, role: 'Software Engineer'}; | ||
|
|
||
| console.log("FUNCTION 2 (via entries): " + convert(obj)); | ||
|
|
||
|
|
||
| // FUNCTION 2: convertation hash to array (via keys and map) | ||
|
|
||
| function convert1(hash) { | ||
| var arrOfKeys = Object.keys(hash); | ||
|
|
||
| return arrOfKeys.map(function(key) { | ||
| return [key, hash[key]]; | ||
| }); | ||
| } | ||
|
|
||
| var obj1 = {name: 'Jeremy', age: 24, role: 'Software Engineer'}; | ||
|
|
||
| console.log("FUNCTION 2 (via keys and map): " + convert1(obj1)); | ||
|
|
||
|
|
||
| // FUNCTION 3: convertation dash/underscore string to camelCase | ||
|
|
||
| function camelCase(str) { | ||
|
|
||
| var sep; | ||
|
|
||
| // check what kind of separator sring has | ||
|
|
||
| str.search('-') > 0 ? sep = '-' : sep = '_'; | ||
|
|
||
| var arr = str.split(sep).map(function(word, ind) { | ||
|
|
||
| if (ind !== 0) { | ||
| return word.charAt(0).toUpperCase() + word.slice(1); | ||
| } | ||
|
|
||
| return word; | ||
| }); | ||
|
|
||
| return arr.join(''); | ||
| } | ||
|
|
||
| console.log("FUNCTION 3: " + camelCase("the-stealth-warrior")); | ||
|
|
||
|
|
||
| // FUNCTION 4: reverse each word in sentence | ||
|
|
||
| function reverse (sentence) { | ||
| var splitSentence = sentence.split(" "); | ||
| return splitSentence.map(function(word){ | ||
| return word.split("").reverse().join(""); | ||
| }).join(" "); | ||
| } | ||
|
|
||
| console.log("FUNCTION 4: " + reverse(" A fun little challenge! ")); | ||
|
|
||
|
|
||
| // FUNCTION 5: the expansion of the string | ||
|
|
||
| function stringExpansion(string) { | ||
|
|
||
| var repeatSymbol = 1; | ||
|
|
||
| return string.split("").reduce(function(acc, symbol) { | ||
|
|
||
| // if the symbol is number | ||
|
|
||
| if (isNaN(symbol) === false) { | ||
|
|
||
| repeatSymbol = +symbol; | ||
|
|
||
| // if the symbol is letter | ||
|
|
||
| } else { | ||
|
|
||
| for (var i = 0; i < repeatSymbol; i++) { | ||
| acc = acc + symbol; | ||
| } | ||
|
|
||
| repeatSymbol = 1; | ||
|
|
||
| return acc; | ||
| } | ||
|
|
||
| return acc; | ||
|
|
||
| },""); | ||
|
|
||
| } | ||
|
|
||
| console.log("FUNCTION 5: " + stringExpansion("a0bcd4gh")); | ||
|
|
||
|
|
||
| // FUNCTION 6: largest and smallest number | ||
|
|
||
| function largest() { | ||
|
|
||
| function compareNumbers(a, b) { | ||
| return b - a; | ||
| } | ||
|
|
||
| var numbers = []; | ||
|
|
||
| // transform arguments into array numbers | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| numbers[i] = arguments[i]; | ||
| } | ||
|
|
||
| numbers = numbers.sort(compareNumbers); | ||
| return numbers[0]; | ||
| } | ||
|
|
||
| function smallest() { | ||
|
|
||
| function compareNumbers(a, b) { | ||
| return a - b; | ||
| } | ||
|
|
||
| var numbers=[]; | ||
|
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. Whitespaces |
||
|
|
||
| // transform arguments into array numbers | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| numbers[i] = arguments[i]; | ||
| } | ||
|
|
||
| numbers = numbers.sort(compareNumbers); | ||
| return numbers[0]; | ||
| } | ||
|
|
||
| console.log("FUNCTION 6 (largest): " + largest(2, 0.1, 500, 100, -3)); //500 | ||
| console.log("FUNCTION 6 (smallest): " + smallest(2, -0.1, -5, 700, -300)); //-300 | ||
|
|
||
|
|
||
| // FUNCTION 7: transform array | ||
|
|
||
| function transform(arr) { | ||
|
|
||
| return arr.map(function(elem) { | ||
| return function() { | ||
| return elem; | ||
| } | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| var baseArray = [10, 20, 30, 40, 50]; | ||
| var newArray = transform(baseArray); | ||
| console.log("FUNCTION 7: " + newArray[3]()); // 40 | ||
| console.log("FUNCTION 7: " + newArray[4]()); // 50 | ||
|
|
||
|
|
||
| // FUNCTION 8: sum of numbers (without recursion) | ||
|
|
||
| function sum() { | ||
|
|
||
| var sumOfArgs = 0; | ||
|
|
||
| for (var i = 0; i < arguments.length; i++) { | ||
| sumOfArgs += arguments[i]; | ||
| } | ||
|
|
||
| return sumOfArgs; | ||
|
|
||
| } | ||
|
|
||
| console.log("FUNCTION 8 (without recursion): " + sum(1, 3, 5, 7)); | ||
|
|
||
|
|
||
| // FUNCTION 8: sum of numbers (recursion) | ||
|
|
||
| function sum1() { | ||
|
|
||
| var i = arguments.length - 1; | ||
| var newArguments = []; | ||
|
|
||
| // transform arguments into array newArguments | ||
| for (var i = 0; i < arguments.length - 1; i++){ | ||
| newArguments.push(arguments[i]); | ||
| } | ||
|
|
||
| if (newArguments.length > 0) { | ||
|
|
||
| return arguments[i] + sum1.apply(null, newArguments); | ||
|
|
||
| } else { | ||
|
|
||
| return arguments[i]; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| console.log("FUNCTION 8 (recursion): " + sum1(1,3,5,7,9)); | ||
|
|
||
|
|
||
| // FUNCTION 9: logs values with one second | ||
|
|
||
| function countDown(number) { | ||
|
|
||
| var interval = number * 1000 + 1000; | ||
|
|
||
| var count = setInterval(function(){ | ||
|
|
||
| console.log("FUNCTION 9: " + number); | ||
| number--; | ||
|
|
||
| }, 1000); | ||
|
|
||
| return setTimeout(function () { | ||
|
|
||
| clearInterval(count); | ||
|
|
||
| }, interval); | ||
|
|
||
| } | ||
|
|
||
| countDown(2); | ||
|
|
||
|
|
||
| // FUNCTION 10: prototype for bind() | ||
|
|
||
| Function.prototype.myBind = function (context) { | ||
| var boundThis = this; | ||
| var args = []; | ||
|
|
||
| //add arguments in args array without context | ||
| for (var i = 0; i < arguments.length-1; i++) { | ||
| args[i] = arguments[i + 1]; | ||
| } | ||
|
|
||
| return function () { | ||
|
|
||
| var innerArgs = []; | ||
|
|
||
| //transform arguments into array innerArgs | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| innerArgs[i] = arguments[i]; | ||
| } | ||
|
|
||
| // join two arrays | ||
| args = args.concat(innerArgs); | ||
|
|
||
| return boundThis.apply(context, args); | ||
| } | ||
| } | ||
|
|
||
| function addPropToNumber(number, number2) { return this.prop + number * number2; }; | ||
|
|
||
| var bound = addPropToNumber.myBind({ prop: 9 }, 5); | ||
|
|
||
| console.log("FUNCTION 10: " + bound(5)); // 34 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Object.entriesis a part of ES6+However I see that you have ES5 version below so it's just a tip for you to be aware of in case you will maintain legacy code
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.
Yes, I know, I recognized it when function was written and decided to save both variants