This question already has answers here:
Callback function - use of parentheses
(4 answers)
Closed last year.
I'm beginner at JavaScript and I need some help with this. Let's say a user has to enter a number from the HTML form. Then in another function there is a calculation, but when I'm calling the other function I'm getting NaN. This is my function when I'm entering the number, and if I output this I'm getting the calculated result.
const enterNumbers = number => {
if (number < 10000) {
return 0.062 * number;
} else {
return 0.062 * 20000;
}
If I output this in a third function let's say
const display = () => {
console.log (enterNumbers((number)));
I'm getting the result, but then in another function I want to make some calculation with the number calculated from the enterNumbers function
const calculation = () => {
return enterNumbers * 10;
}
and then when I call the second function in the display function I'm getting a NaN.
What happens if you call?
const calculation = () => {
return enterNumbers(<ANY_NUMBER_HERE>) * 10;
}
Related
This code is confusing me.
Where does the inner function get the value for x.
function createMultiplier(multiplier) {
return function(x) {
return x * multiplier
}
}
let doubleMe = createMultiplier(2);
This is called currying, what happens is that in the function createMultiplier you get a function and then when you execute that function you pass the parameter x
If you want to execute the function inside the function immediately, you can use this code createMultiplier(2)(5)
If you use console logs you can understand better what happens, I made an example bellow, so you can see first you only get a function and then you can pass the parameter x
function createMultiplier(multiplier) {
return function(x) {
return x * multiplier
}
}
const doubleMe = createMultiplier(2);
console.log(doubleMe)
const result = doubleMe(5)
console.log(result)
The inner function will get the value x. When you invoke: doubleMe(16), for example.
When you call createMultiplier(2) you are setting the multiplier value.
It might be helpful to try and visual what happens when you call createMultiplier(2).
This will create a function that would look like this:
function doubleMe(x) {
return x * 2; // 2 is the multiplier that we supplied to 'createMultiplier'
}
let answer = doubleMe(4);
// answer = 8;
another example:
let tripleMe = createMultiplier(3);
// is the same as writing this
function tripleMe(x) {
return x * 3;
}
let answer = tripleMe(3);
// answer = 9;
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
i have a function , i need to delay its return until a value is set (by calling another function call_api basically i need to wait for second function result to be able to return the first function result )
here is what ti got
function getList(s, p)
{
var output = false ;
call_api(s , p , function(result){
console.log(result);
output = result;
})
while (output == false )
{
1 ;
}
return output ;
}
but it wont work while somehow is messing thing up
is there a way to solve this ?
ps : i know about promise / async function but i cant use them (its a test i ant use them in the test )
You could do it using Promises, using a callback, or by checking the value every interval, using setTimeout. You can't do otherwise by definition of the Javascript language.
Using promises:
async function getList(s, p) {
return await new Promise(resolve => call_api(s, p, result => resolve(result)));
}
Using callback:
var output = false;
call_api(s, p, result => {
output = result;
callback();
});
function callback() {
// rest of code
}
Using setTimeout:
var output = false;
call_api(s, p, result => output = result);
checkOutput();
function checkOutput() {
if (output === false) {
setTimeout(checkOutput, 100);
} else {
// rest of code
}
}
This question already has answers here:
"add" function that works with different combinations of chaining/arguments
(4 answers)
Closed 3 years ago.
I was trying to solve a puzzle which takes 'n' arguments and then computes sum.
function add(arg1) {
let total=arg1;
const func = function(...args){
if(!args[0]) return total;
total += args[0];
return func;
}
return func;
}
console.log(add(1)(2)(3)()); // => Should output 6
Question: Using the same logic above example, I am trying to do this:
What if I want to do something like
sum(1)(2)(3) => should give 6
If i do sum(1) => Should output 1
sum(1)(2) => should output 3 and so
forth.
This is what I've tried:
function sum2(num) {
let total = num;
const fn = function(...args) {
if(args[0]) total += args[0];
return total;
}
return fn();
}
console.log(sum2(1)) //=> Works
console.log(sum2(1)(2))// => Fails
Explanation:
Proxy : The Proxy object is used to define custom behaviour for fundamental operations. In javascript you can create traps for pretty much every operation
Used Getter function of the handler to return Sum
Used Apply function of the handler to iterate and perform addition.
function sum2(n) {
sum = n;
const adhoc = new Proxy(function a () {}, {
get (obj, key) {
return () => sum;
},
apply (receiver, ...args) {
sum += args[1][0];
return adhoc;
},
});
return adhoc
}
console.log(sum2(1));
console.log(sum2(1)(2));
console.log(sum2(1)(2)(3));
I'm trying to solve this algorithm. I think I'm very close.
I need the seven function to pass the a variable to the timesFive function. OR is there a way I can pass the * 5 operation from timesFive to seven in order to get the desired result?
I'm not allowed to have variables outside of these two function scopes.
I can add another argument to timesFive as long as b remains the first argument.
function seven(action) {
var a = 7;
return action;
}
function timesFive(b) {
console.log(a); // How can I access "a"?
return a * 5;
}
console.log(seven(timesFive())); // Desired result: 35
timeFive should return a function and seven should expect a function as a parameter. Then seven can simply call the function that timesFive() returns (so basically returning the * 5 operation as you mentioned in your question):
function seven(action) {
var a = 7;
return action(a);
}
function timesFive() {
return (a) => a * 5;
}
console.log(seven(timesFive())); // Desired result: 35
BTW
You'll often see these kind of exercises written in a way that mimics the functional notation from math, like:
const seven = (action) => action(7);
const timesFive = () => (a) => a * 5;
console.log(seven(timesFive())); // Desired result: 35
I want to write a function called twice that takes a function f and a value x as its parameters and returns f(f(x)). For example,
twice(function (x) { return x * x; }, 3)
should return 81. How do I do it?
Call the anonymous function twice which is passed as an argument. On the second call use the returned value of first execution as the argument.
function twice(funct, a) {
return funct(funct(a));
}
console.log(
twice(function(x) {
return x * x;
}, 3)
);
What is stopping you? You already has the answer with you. It is working pretty fine.
function twice(f, x) {
return f(f(x));
}
var result = twice(function(n){return n*n;}, 3);
console.log(result);