JavaScript function - javascript

function hi(a, b) {
return a * b
}
function hello(a, b) {
return hi(a, b + 2)
}
console.log(hello(2, 3)); // 10
I do not understand how the answer is 10. Would someone be able to help me?

function hi(a, b) {
return a * b
}
function hello(a, b) {
return hi(a, b + 2)
}
hello(2, 3)
//10*
I strongly believe, the * you used in the start is an error so the above is the correct beautified code
You are calling function hello with 2 arguments, 2 & 3 which calls hi function with argument (2, 5) as 3 + 2 is 5
When it will call hi, it will multiple 5*2 which is 10

Let's first you understand what you have written by yourself. There are two functions hi & hello
function hi(a, b) {
return a * b
}
function hello(a, b) {
return hi(a, b + 2)
}
hello(2, 3)
When the following function is executed
hello(2,3) returns hi(2,3+2).
hi(2,5) returns 2*5. Thus the answer is 10.

When you call the function with argument 2 and 3. These values get assigned to respective parameters.
a = 2 and b = 3
then inside the function hello you are returning the value of what hi function returns with arguments as
hi(a, b + 2)
// here what you've passed argument to hi is as follows:
// a => value of a => 2
// b => value of b + 2 => 3 + 2 => 5
so when hi executes it returns
a * b
2 * 5 = 10

Related

Is there a point in passing arguments(variables) to a function which can already access them from its closure?

For example:
let a = 5;
let b = 6;
function sum1() {
console.log(a + b);
}
function sum2(a, b) {
console.log(a + b);
}
Are there any cases where one of the examples won't work?
In sum1 as you can’t pass arguments, it will do 5 + 6.
But in sum2 you can do something like this:
sum2(7,8) >> 15
sum2(1,5) >> 6
It’s better sum2, because use call it from anywhere and passing it arguments, you want.

Dont understand the next iteration for recursion

I'm learning to work with recursion and so far it went well with basic examples. However I want to calculate the factorial, but I don't understand what happened step-by-step. If I do it imperatively I understand it but I fail on this example:
return x * fac(x-1); gives me back 5 * 4, so far soo good, but what happens now? Does it mean it become 20 now? So my next iteration would be then 20 * 19?
const fac = (x) => {
if(x <= 1) {
return x;
}
return x * fac(x-1);
};
console.log(fac(5)); // 120
just walk through the logic.
1. fac(5) yields 5 * fac(4)
2. fac(4) yields 4 * fac(3)
3. fac(3) yields 3 * fac(2)
4. fac(2) yields 2 * fac(1)
5. fac(1) yields 1
substituting from bottom to top, you get
fac(5) = 5 * 4 * 3 * 2 * 1
I think i understand that part now and the difference between 4 and fac(4). The steps look like:
5. 5 * // ()
4. 4 * // (20)
3. 3 * // (60)
2. 2 * // (120)
1. 1 * // (120)
However, I have another example which i cant resolve step by step, while i can see the logic in the imperatively programming.
let fibo = (x) => {
if(x<=2) {return 1;}
return fibo(x-1) + fibo(x-2);
};
console.log(fibo(4)); //3
I cant resolve step by step what return fibo(x-1) + fibo(x-2); values this gives me each step. On the imperatively programming it is
function fibonacci(num){
var a = 1, b = 0, temp;
while (num >= 1){
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
console.log(fibonacci(4); // 3
where the steps would be like
4. b = 1
3. b = 1
2. b = 2
1. b = 3

How to right a variable length currying function or a general currying function for all type of inputs? [duplicate]

This question already has answers here:
Create function to add such that add(1,2)(3,...k)(1,2,3)...(n) should sum all numbers [duplicate]
(1 answer)
"add" function that works with different combinations of chaining/arguments
(4 answers)
Closed 3 years ago.
function currying(func) {
//I need to complete this for all diiferent forms of add
}
//1st form
const add = currying(function (a, b) {
return a + b;
})
add(1, 2) //should yield 3
add(1)(2) //should yield 3
//second form
const add = currying(function (a, b, c) {
return a + b + c;
})
add(1, 2)(3) //should yield 6
add(1)(2)(3) //should yield 6
add(1, 2, 3) //should yield 6
//third form
const add = currying(function (a, b, c, d) {
return a + b + c + d;
})
const a11 = add(1)
a11(2)(3)(4) //should yield 9
a11(2, 3, 4) //should yield 9
How to complete the top most "currying" function for all these cases? "Currying" function must return correct answer for any of these kind of function calls.

addOne takes in a number as a parameter // and returns that numbers +1?

Function addOne returns undefined result...
const t = 5;
const b = 8;
function addOne () {
add (t + b);
return addOne;}
Pls help to get a sum of 5 + 8 with this function.
const t = 5;
const b = 8;
function addOne () {
return t+b;
}
console.log(addOne()); //13
I am not sure what you are asking, since your method's name is not matching with your implementation. But here are some code that might help you:
const t = 5;
const s = 8; // Note that I have renamed the parameter's name
function addOne () {
return s + 1;
}
function addOneToParameter(x) {
return x + 1;
}
function addTwoConstants () {
return t + s;
}
function addTwoParameters (a, b) {
return a + b;
}
console.log(addOne()); // 9 (The constant 's' + 1)
console.log(addTwoConstants()); // 13 (The constant 's' + constant 't')
console.log(addTwoParameters(1,2)); // 3 (The first parameter + the second parameter)
console.log(addOneToParameter(4)); // 5 (The parameter + 1)
In your original question, you don't need to use a function to perform an addition of two integers. In other words, simply use '+' as operand. Furthermore, pay attention how to return a value from your function.

_.memoize only cache's first argument

I realized today that the _memoize function only caches results for the first argument provided.
function add(a, b) {
return a + b;
}
var sum = _.memoize(add);
console.log(sum(1,2));
>>> 3
console.log(sum(1,5));
>>> 3
Is this a bug or deliberate?
Deliberate. Relevant docs:
The default hashFunction just uses the first argument to the memoized function as the key.
But good news is you can change this behaviour by introducing your own hash function.
function myInefficientHashFunction() {
// not really an efficient hash function
return JSON.stringify(arguments);
}
function add(a, b) {
return a + b;
}
var sum = _.memoize(add, myInefficientHashFunction);
document.getElementById('one_two').textContent = sum(1, 2)
document.getElementById('one_three').textContent = sum(1, 3)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<div>1 + 2 = <span id="one_two"/></div>
<div>1 + 3 = <span id="one_three"/></div>
const g = _.memoize(([a, b]) => a + b)
console.log(g([1, 2]))
console.log(g([1, 3]))
console.log(g([1, 4]))
pass all as the first argument

Categories

Resources