Curry Function - 4 | Hard | JavaScript Interview Question - 6

preview_player
Показать описание
JavaScript Interview Question - 6 | In this video, we will see how to solve a simple JavaScript problem where we are asked to implement a curry function.

The curry function can keep on accepting arguments in the same or subsequent calls by returning a function. When the value() method is invoked or any primitive operation is performed then only the curry function returns the sum of all the arguments it has received.

Get my Ebook "JavaScript Interview Guide" with 120+ solved JavaScript questions.

Social links
Рекомендации по теме
Комментарии
Автор

i think the following line was redundant -
(lineNo.28) "this.value = helper.valueOf"

because sum function is always returning the helper function.
Therefore, (lineNo.34) - "console. log (add(1, 2, 3). value() == 6);" will call helper.value not sum.value, so there is no edge case to cover here.

Thank you, enjoying the playlist.

lalitsheoran
Автор

function add(...a) {
function recursive(...b) {
total += b.reduce((sum, curr) => (sum += curr), 0);

return recursive;
}

let total = a.reduce((sum, curr) => (sum += curr), 0);

function returnTotal() {
return total;
}
recursive.valueOf = returnTotal;
recursive.value = returnTotal;

return recursive;
}

console.log(add(1)(2)(3, 4).valueOf());

My approach

hritickjaiswal
Автор

Why do we need this value method explicitly, the add function also returns the sum only, right?! 😕

PallaviNagarkar