-
Notifications
You must be signed in to change notification settings - Fork 12
HomeWork3 - js-1 #9
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
85351f9
c54d6f9
9d5562e
67ead80
119c7a3
1c4435d
3c77d03
04c08bd
8825d57
002c657
e844808
3dfbeae
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,16 @@ | ||
| /*1) Write a function splitAndMerge | ||
|
|
||
| Function accept 2 parameters:str and sp. str is a sentence. sp is a char as separator. First we need to divide the sentence into words(Use separator space); and then divide each word into characters(Use separator empty string); and then merge each characters with the specified sp; at last merge all the words(Use separator space) and return it. | ||
|
|
||
| Example: | ||
|
|
||
| splitAndMerge("My name is John"," ") should return "M y n a m e i s J o h n" | ||
| splitAndMerge("Hello World!",",") should return "H,e,l,l,o W,o,r,l,d,!" */ | ||
|
|
||
| function splitAndMerge (str, sp) { | ||
| var target=str.split(' ').join('').split('').join(sp); | ||
| return target | ||
| } | ||
| console.log(splitAndMerge("Hello there!",",")) | ||
| console.log(splitAndMerge("General Kenobi"," ")) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /*10) Write a polyfill for a .bind() function and save it in Function.prototype.myBind(). myBind() should work in an exact same way as the usual bind() - take context as a first parameter and the list of arguments separated by comma. | ||
|
|
||
| Hint: play with the function in Function.prototype and see what this points to inside it. Your code should look like: Function.prototype.myBind = function () { | ||
| // your code here | ||
| } | ||
|
|
||
| Example: | ||
|
|
||
| function addPropToNumber(number) { return this.prop + number; } | ||
| var bound = addPropToNumber.myBind({ prop: 9 }); | ||
| bound(1) // 10*/ | ||
|
|
||
| Function.prototype.myBind = function () { | ||
| var target = this; | ||
| var args = arguments[0]; | ||
| var curArgs = [].slice.call(arguments, 1); | ||
| return function() { | ||
| var newArgs = [].slice.call(arguments); | ||
| var targetArgs = curArgs.concat(newArgs); | ||
| return target.apply(args, targetArgs); | ||
| } | ||
| } | ||
|
|
||
| function addPropToNumber(number) { return this.prop + number; } | ||
| var bound = addPropToNumber.myBind({ prop: 10 }); | ||
| console.log(bound(1)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /*2) Write a function convert | ||
|
|
||
| Convert a hash into an array. Nothing more, Nothing less. | ||
| {name: 'Jeremy', age: 24, role: 'Software Engineer'} | ||
| should be converted into | ||
| [["name", "Jeremy"], ["age", 24], ["role", "Software Engineer"]]*/ | ||
|
|
||
| function convert(hash) { | ||
| var values=[] | ||
| for (value in hash) { | ||
| values.push([value, hash[value]]); | ||
| } | ||
| return values; | ||
| } | ||
| console.log(convert({name: 'Anatoly', age: 32, role: 'Wannabe front-end developerx'})); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /*3) Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized. | ||
|
|
||
| Example: | ||
|
|
||
| toCamelCase("the-stealth-warrior") // returns "theStealthWarrior" | ||
| toCamelCase("The_Stealth_Warrior") // returns "TheStealthWarrior" */ | ||
|
|
||
| function toCamelCase(str) { | ||
| target=str.split(''); | ||
| target.reduce(function(acc, cur, n, arr) { | ||
| if (cur == '-' || cur == '_') { | ||
| arr[n+1] = arr[n+1].toUpperCase(); | ||
| arr.splice(n,1); | ||
| return acc; | ||
| } | ||
| return acc += cur; | ||
| }) | ||
| return target.join(''); | ||
| } | ||
| console.log(toCamelCase("the-stealth-warrior")); | ||
|
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. 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.
Author
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. Fixed. Forgot about |
||
| console.log(toCamelCase("The_Stealth_Warrior")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /*4) Write a function that takes a sentence (string) and reverses each word in the sentence. | ||
|
|
||
| Example: | ||
|
|
||
| " A fun little challenge! " => " A nuf elttil !egnellahc "*/ | ||
|
|
||
| function reveresestring(str) { | ||
| return str.split("").reverse().join("").split(" ").reverse().join(" "); | ||
| } | ||
| console.log(reveresestring(" A fun little challenge! ")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /*5) Write a function stringExpansion | ||
|
|
||
| Given a string that includes alphanumeric characters ('3a4B2d') return the expansion of that string: The numeric values represent the occurance of each letter preceding that numeric value. There should be no numeric characters in the final string. Empty strings should return an empty string. | ||
|
|
||
| The first occurance of a numeric value should be the number of times each character behind it is repeated, until the next numeric value appears. | ||
| stringExpansion('3D2a5d2f') === 'DDDaadddddff' | ||
| If there are two consecutive numeric characters the first one is ignored. | ||
| stringExpansion('3d332f2a') === 'dddffaa' | ||
| If there are two consecutive alphabetic characters then the first character has no effect on the one after it. | ||
| stringExpansion('abcde') === 'abcde' | ||
| Your code should be able to work for both lower and capital case letters.*/ | ||
|
|
||
| function stringExpansion(str) { | ||
| arr=str.split(''); | ||
| var mul=1; | ||
| var target=[]; | ||
| for (var i=0; i<arr.length; i++) { | ||
| if (!isNaN(Number(arr[i]))) { | ||
| mul=arr[i]; | ||
| } | ||
| if (isNaN(Number(arr[i]))) { | ||
| for (n=0; n<mul; n++) { | ||
| target.push(arr[i]); | ||
| } | ||
| mul=1; | ||
| } | ||
| } | ||
| return target.join(''); | ||
| } | ||
|
|
||
| console.log(stringExpansion("3D2a5d2f")); | ||
| console.log(stringExpansion("3d332f2a")); | ||
| console.log(stringExpansion("abcde")); | ||
| console.log(stringExpansion("abc3D4e")); | ||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| 6) Write largest and smallest functions that returns the largest and smallest number passed like a argument. | ||
|
|
||
| Example: | ||
|
|
||
| largest(2, 0.1, -5, 100, 3) // 100 | ||
| smallest(2, 0.1, -5, 100, 3) // -5 */ | ||
|
|
||
| function smallest() { | ||
| return Math.min.apply(this,arguments) | ||
| } | ||
| console.log(smallest(2, 0.1, -5, 100, 3)); | ||
|
|
||
| function largest() { | ||
| return Math.max.apply(this,arguments) | ||
| } | ||
| console.log(largest(2, 0.1, -5, 100, 3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /*7) Write function transform that will transform array of numbers to array of functions that will return value from a base array. | ||
|
|
||
| Example: | ||
|
|
||
| const baseArray = [10, 20, 30, 40, 50]; | ||
| const newArray = transform(baseArray); | ||
| newArray[3](); // should return 40 | ||
| newArray[4](); // should return 50*/ | ||
|
|
||
|
|
||
| function transform (arr) { | ||
| return arr.map(function(target) { | ||
| return function() { | ||
| return target; | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| const baseArray = [10, 20, 30, 40, 50]; | ||
| const newArray = transform(baseArray); | ||
| console.log(newArray[3]()); // should return 40 | ||
| console.log(newArray[4]()); // should return 50 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /*8) Write function sum. Function expects arbitrary number of digit arguments and returns compound value of them. | ||
|
|
||
| Note: function should use recursion | ||
|
|
||
| Example: | ||
|
|
||
| sum(1,3,5,7); //should return 16 */ | ||
|
|
||
| function sum() { | ||
| if (arguments.length === 0) { | ||
| return 0; | ||
| } else { | ||
| return arguments[0] + sum.apply(0, Array.prototype.slice.call(arguments, 1)); | ||
| } | ||
| } | ||
| console.log(sum(1,3,5,7)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /*9) Write function countDown. Function expects number and logs values one by one till zero with one second delay. | ||
|
|
||
| Example: | ||
|
|
||
| countDown(3); // 3 2 1 0*/ | ||
| function countDown(num) { | ||
| var counter=setInterval(function () { | ||
| console.log(num); | ||
| num = num-1; | ||
| if (num<0) { | ||
| clearInterval(counter); | ||
| } | ||
| }, 1000); | ||
| } | ||
| countDown(3); |


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.
Current output is
H,e,l,l,o,t,h,e,r,e,!instad ofH,e,l,l,o t,h,e,r,e,!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.
but... well... it seems to me that i've been confused with this
splitAndMerge("My name is John"," ") should return "M y n a m e i s J o h n"(no extra spaces here)
am i missing some logic of how function should work?