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));
Related
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 1 year ago.
Improve this question
I'm trying to get these functions defined to run some synchronous and asynchronous test cases on them with Mocha and Chai in JS, what am I doing wrong? Why is my editor marking certain lines?
module.exports = {
function myFunctiona () {
}
function myFunctionb () {
for (let i = 0; i < 10000; i++) {
new Date();
}
}
function myFunctionc(done) {
setTimeout(done, 0);
}
function myFunctiond (done) {
setTimeout(done, Math.round(Math.random() * 10));
}
}
This is a syntax error because you're defining an object with properties, but you don't have property keys. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_colon_after_property_id for more information.
Typically, you'd define an object like the following
(note the commas after each property too):
var object = {
property1: 'thing',
property2: function() {
return 'thing2';
}
}
so to change your functions to properties, set the property key as the function name, and then assign a function to it like:
module.exports = {
myFunctiona: function () {
//nothing
},
myFunctionb: function () {
for (let i = 0; i < 10000; i++) {
new Date();
}
},
myFunctionc: function (done) {
setTimeout(done, 0);
},
myFunctiond: function (done) {
setTimeout(done, Math.round(Math.random() * 10));
}
};
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
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
What is wrong with this code?
function makeGamePlayer(int) {
var bal = 0;
var obj = {
fun1: function () {
bal += int;
},
fun2: function () {
return bal;
}
};
return obj;
}
console.log(makeGamePlayer(100)); //obj
console.log(makeGamePlayer(100).fun2()); //returning 0 but expecting the 100
I tried running the above code but I'm not getting the correct results. I need some help.
Thanks in advance.
The ONLY way to make the second console.log output 100 without changing the makeGamePlayer code at all is as follows
function makeGamePlayer(int) {
var bal = 0;
var obj = {
fun1: function () {
bal += int;
},
fun2: function () {
return bal;
}
};
return obj;
}
// commented, as it's irrelevant
//console.log(makeGamePlayer(100)); //obj
var x = makeGamePlayer(100); // x is a **different obj and bal by the way**
x.fun1();
console.log(x.fun2());
// to illustrate that each time you call `makeGamePlayer` you get a new obj and a new bal
var y = makeGamePlayer(1000);
y.fun1();
// note, this is STILL 100
console.log(x.fun2());
// this outputs 1000
console.log(y.fun2());
// output
// 100
// 100
// 1000
You are incrementing int in fun1 function, but you haven't called in anywhere. You have called fun2 function which is returning the bal as it is.
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 8 years ago.
Improve this question
My checkforZeroQuantity function is not getting called if I use it with if. Following is the sample code
function myButtonClicked() {
if (checkforZeroQuantity()) {
alert("checked");
}
}
function checkforZeroQuantity() {
var x = 1;
if (x == 0) {
return false;
} else {
retrun true;
}
}
This is because you have spelling mistake near as shown below
else {
retrun true; // it should be return
}
correct the spelling and try again.
look here:
function checkforZeroQuantity() {
var x = 1;
if (x == 0) {
return false;
} else {
return true;
}
}
There spelling mistake in code, please change 'retrun' to 'return'.
else
return true;
Firstly, there is a typo in the else block.:)
Secondly, it's a good practice to use === instead of == in Javascript
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For some reason, the evaluator if isEven(n) is giving an error... can you spot an issue with the code?
function isEven(value) {
//value = Number(value);
if (value%2 == 0)
return true;
else
return false;
}
function testCondition {
if isEven(n) {
}
else {
}
}
There are two syntax errors in your code:
function isEven(value) {
//value = Number(value);
if (value%2 == 0) // no parens here is allowed so no syntax error
return true;
else
return false;
}
function testCondition() {
// ^^ missing parens here
if (isEven(n)) {
// ^ ------- ^ -- and here
}
else {
}
}
if isEven(n) {
You're missing parenthesis around this, so you'll get a SyntaxError
if (isEven(n)) {
You're also missing a () after testCondition, as pointed out by Frits van Campen.
Just some minor syntax issues, try this:
function isEven(value) {
//value = Number(value);
if (value%2 == 0){
return true;
}else{
return false;
}
}
function testCondition() {
if (isEven(n)) {
//do something
} else {
//do something else
}
}