Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I can't seem to wrap my head around the logic for writing a function that looks something like eight(times(five)) in JavaScript.
Example:
eight(times(five)) should return 40
three(plus(two)) should return 5
It was a Codewars problem. It provided empty functions for numbers 0-9 that looked like "eight( ){ }" as well as empty functions for operators that looked like "times( ){ }." The solution should be such that executing "eight(plus(three))" would return 11, etc.
You could use a function which returns either a value or a function call with the actual value. Then you nee a function for the operand which retuns a closure over the function for the value.
function eight(fn) {
return typeof fn === 'function' ? fn(8) : 8;
}
function times(fn) {
return function (op) {
return fn() * op;
};
}
console.log(eight(times(eight)));
I know the question got answered while i was writing this reply...but i will post it anyways.
function times(fn) {
return function (op) {
return fn() * op;
};
}
function plus(fn) {
return function (op) {
return fn() + op;
};
}
function minus(fn) {
return function (op) {
return fn() - op;
};
};
createNumbers = () => {
var num = 0;
['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'].forEach((number) => {
let val = num;
window[number] = (fn) => {
return fn ? fn(val) : val;
};
num++;
});
};
createNumbers();
three(times(five)); // 15
eight(plus(two));// 10
etc
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If I want to run my function again inside that same function when a certain condition is met, what's the difference between returning that function VS just calling that function again.
function myFunc(param) {
if (param === "3") {
return myFunc(param);
}
}
VS
function myFunc(param) {
if (param === "3") {
myFunc(param);
}
}
If you need to return a result, then you need to return the result of the inner call recursively. For example:
function fact(n) {
if (n === 0) return 1;
return n * fact(n - 1);
}
console.log(fact(5));
But if the recursive function accomplishes what it needs to only inside the function - it's all side-effects from inside - then there's no need to return:
function addSpans(container, n) {
const span = container.appendChild(document.createElement('span'));
span.textContent = n;
if (n >= 1) addSpans(span, n - 1);
}
addSpans(document.body, 3);
return myFunc() does three things: (1) execute the function myFunc(), (2) when 1 finishes, take the result value and assign it as the return value of the current function, and (3) terminate the current function.
Calling myFunc() only does (1). That is the difference.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
function trycatch(myfunction, name) {
return function() {
try {
myfunction.apply(this, arguments);
} catch (e) {
console.log(e + name);
}
}
};
var rambo = {
create: function(i, j) {
return i
},
ramv: function() {
aler("")
}
};
for (var key in rambo) {
if (typeof rambo[key] === 'function') {
rambo[key] = trycatch(rambo[key], key);
}
}
console.log(rambo.create(1))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I am trying to apply try catch to all my functions , but it seems they are not returning the values , Am i missing something?
The function you return from trycatch has no return statement, so it will always return undefined.
You need to return the return value of your call to apply.
You have forgot to add return in myfunction.apply(this, arguments);. Currently the trycatch returns the function but that function do not return anything so you also need to return the function value of myfunction.apply(this, arguments);
function trycatch(myfunction,name) {
return function() {
try {
return myfunction.apply(this, arguments);
} catch(e) {
console.log(e + name);
}
}
};
var rambo = { create: function(i,j) { debugger; return i},
ramv:function(){aler("")}
};
for (var key in rambo) {
if( typeof rambo[key] === 'function' ) {
rambo[key] = trycatch(rambo[key],key);
}
}
console.log(rambo.create(1));
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is this a smart alternative to creating a object of functions? Benefits? Downsides?
var _ = require("underscore")
function functionName(fn){
//http://stackoverflow.com/a/17923727/340688
return /^function\s+([\w\$]+)\s*\(/.exec(fn.toString())[1]
}
function objecfify(arr){
return _.chain(arr)
.map(function(fn){
return [functionName(fn), fn]
})
.object()
.value()
}
var model = objecfify([
function create(){
return "create"
},
function read(){
return "read"
},
function update(){
return "update"
},
function remove(){
return "delete"
}
])
console.log(model)
Try to use fn.name to get function name:
var functions = [
function create(){
return "create"
},
function read(){
return "read"
},
function update(){
return "update"
},
function remove(){
return "delete"
}
];
var result = functions.map(function(fn) {
return fn.name;
});
document.write(JSON.stringify(result));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I usually do something like the following when dealing with variadic arguments in JavaScript:
var f = function() {
var args = Array.prototype.slice.call(arguments, 0);
// ...
return something;
};
But what about doing instead:
var f = function() {
return (function(self, args) {
// ...
return something;
}(this, Array.prototype.slice.call(arguments, 0)));
};
I couldn't readily find anything addressing the pattern above, so I decided to post it here. Before I start experimenting with it, I would like to know if there are any flaws or risks I could be missing when using the rather unusual patterns described above.
I have found the pattern to be slightly less readable than the first, but it lets me write longer one-liners without declaring variables so I am growing fond of it.
Object.defineProperty(RegExp, 'join', {
value: function() {
return (function(regexes, last) {
return RegExp(regexes.map(function(item) {
return item.source;
}).reduce(function(prev, next) {
return prev + next;
}), last.slice(last.lastIndexOf('/') + 1));
}(Array.prototype.slice.call(arguments, 0),
arguments[arguments.length - 1]+''));
}, enumerable: false
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some functions, like:
function functionOne() {
/*
code
*/
}
function functionTwo() {
/*
code
*/
}
function functionThree() {
/*
code
*/
}
How can I write code to call each of the functions, once each (i.e. without repeats), in a random order?
Put your functions in an array, randomise the array and then iterate through it:
var functions = [
function () { alert("function 0"); },
function () { alert("function 1"); },
function () { alert("function 2"); },
function () { alert("function 3"); },
function () { alert("function 4"); }
];
functions.sort(function() { return 0.5 - Math.random() });
for (var i=0;i<5;i++) {
console.log(i);
functions[i]();
}
Here's a fiddle
this function should work:
function randCall(){
var array = [1,2,3];
while(array.length > 0){
var rand = (Math.random*array.length)|0; //converts to an Integer value
switch(array[rand]){
case 1:
functionOne();
break;
case 2:
functionTwo();
break;
case 3:
functionThree();
break;
}
array.splice(rand, 1);
}
}
hope this works and helps.