The answer is [2,2,2,2,2,2,2,2,2,2,2,2,2,2,]
Explain the code step by step and why the answer is above
const sum = (x,y) => x+y;
const add1 = (x) => sum(x,1);
const add2 = (x) => flip(x,1);
const flip = (x,y) => {y =[x, x=y][0]; return add1(x);}
alert([2,3,4,5,6,7,8,9,10,12,14,15,18,21].map(add2))
Let me explain this:
Map function is called on Array [2,3,4,5,6,7,8,9,10,12,14,15,18,21] with method "add2" on each element.
Let's pick an element one by one:
First element is 2 which is passed in "add2" method as argument, "add2" method is calling flip method with argument (2,1). Now flip method is doing the actual trick. in start x =2 , y =1
y = [x,x=y][0]
above line have three statements actually. x will be 1 and y will be assigned value of [2,1][0] which will be 2
1) first value of x is used as 2 .output will be [2,x=y][0].
2) then x =y assigned x to 1. output will be [2,1][0].
3) then y is assigned to 2 because [a,b] [0] will be a & [a,b][1] will be 1.
So now flip method will call "add1" method with parameter x which is having value 1 which in turn calls sum(1,1) which is giving output 2.
Second element is 3 which is passed in "add2" method as argument, "add2" method is calling flip method with argument (3,1). Now flip method is doing the actual trick. in start x =3 , y =1
y = [x,x=y][0]
above line have three statements actually. x will be 1 and y will be assigned value of [3,1][0] which will be 3.
1) first value of x is used as 3.output will be [3,x=y][0]
2) then x =y assigned x to 1. output will be [3,1][0].
3) then y is assigned to 3 because [3,1][0] will be 3.
So now flip method will call "add1" method with parameter x which is having value 1 which in turn calls sum(1,1) which is giving output 2.
so for each element of array, finally value of x is always 1 which is why you are getting output of 2 always.
//below screen shot is just for your testing and understanding when you change the value to y in flip method's final return statement
you can see the difference by just replacing x with y in flip method "return add1(y);" it will give you output as below:

I hope I am able to clarify your doubts. Comment me if you require further help.
Please rate your answer. Thanks
The answer is [2,2,2,2,2,2,2,2,2,2,2,2,2,2,] Explain the code step by step and why the answer is above...
I'm having a hard time writing code for vhdl. Trying to get a 4 bit multipiler with full adder, however I keep getting errors. Was wondering if you can help me with my code. The question is to design a 4-bit multiplier in VHDL by using component statements. library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; --entity declaration entity multi is port( A: in std_logic_vector (3 downto 0); B: in std_logic_vector (3 downto 0); P: out std_logic_vector (7 downto 0)...
List the five implementation models of parameter passing. Consider the following closure in JavaScript: 1- function makeAdder(x) { 2- return function(y) {return x + y;} 3- } . . . 4- var add1 = makeAdder(0); 5- var add2 = makeAdder(10); 6- document.write(add1(20)); 7- document.write(add2(20)); Questions: a) Show the output. b) What is the closure subprogram in this example? c) Explain how the variable x referenced at line 2 is bound.
Study the c++ code below and answer the question on templates after // A polymorphic swap function for any type of object (an example) template<typename T> void MySwap( T& x, T& y) { T temp; temp = x; x = y; y = temp; } // A polymorphic class holding an [x,y] position of any type template<class T> class Position { public: Position(T x=0, T y=0) : x_(x), y_(y) {} friend std::ostream& operator<<(std::ostream& os, const Position& p) { return os...
Working with pointers in C++. I need a brief step by step explanation of the code to help me understand why the output is: 5 4 3 2 1 What will be displayed? Explain each step of the code and the related use of pointers. #include <iostream> using namespace std; int* myfun(int*); int main() { int x[5] = { 1, 2, 3, 4, 5 }; int i, *p; p = myfun(x); for (i = 0; i < 5; i++) ...
Please help me figure out why my code is not working properly.I had thought my logic was sound but later found it will run one time through fine however if the user opts to enter another value it will always be returned as an error. #include <iomanip> #include <iostream> #include <cstdlib> using namespace std; int const TRUE = 1; int const FALSE = 0; // declares functions void GetZipcode(); void RunAgain(); int GetSum(char d1, char d2, char d3, char d4,...
Why is the assertion failing? what code can fix the problem and
where should that code be located at?
class Point { public: Point() { myX = myY = 0; } Point(int n, int y) { myx = x; myy = y; } int getX() const { return myX; } int getY() const { return myY; } virtual void read(istream& in) { in >> myX >> myY; } private: int myx, myY; }; class Point3D : Public Point { public:...
Please provide step by step answer for this JAVA program. I need it in details. (The correct answer is "10") int[] numbers = {1,4,5,0,7,8}; int k = 0; int sum = 0; while(numbers[k] != 0){ sum += numbers[k]; k++; } What is the output of the above code? 25 10 0
Please explain step by step on how you get your answer
b) 2 - th(y)+ Solve the following differential equations: dy 5 + 2x a) 1 - v3y dy c) ; y(2) = 0 dx y 21 dy 1 + y d) My(0) = -1 2 dt 1 + 2 y dx 1 + x dy dy y e) X + 2 y = x'; x > = x'; x > 0; y(1) = 0 f) X = x; x...
please draw a graph to help explain the answer step by step.
thanks a lot.
7. Consider the bivariate random variable (X,Y) which has joint probability density function 1 f(x,y)(x, y) į, for 0 < x,y<1, for – 1 < x, y = 0, 0, elsewhere. 1 2' (a) Derive the marginal probability density functions for X and Y. (b) Evaluate the following probabilities: (i) P(X>0,Y > 0); (ii) P(X > 1/2, Y < 1/2); (iii) P(X + Y <...
the
answer should be in C
What is the output of the following: int whatIsThat(int * x); int main() int hold; int x[] = { 2,4,6,8); hold = whatIsThat(x); printf("%d, %d\n", x[0], hold); return 0; } int whatIsThat(int * a) { int i,sum=0; a[0] = a[O] + 6; for(i = 0; i <= 2; i++) (sum+= a[i]; a[i] = a[i] + 12; return sum; a. 20, 20 b. 20, 18 c. 18, 18 d. none of above What is the...