Question

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...

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))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • List the five implementation models of parameter passing. Consider the following closure in JavaScript: 1- function...

    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...

    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...

  • Please help me figure out why my code is not working properly.I had thought my logic...

    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,...

  • Working with pointers in C++. I need a brief step by step explanation of the code...

    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++)        ...

  • Why is the assertion failing? what code can fix the problem and where should that code...

    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 explain step by step on how you get your answer b) 2 - th(y)+ Solve...

    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....

    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 *...

    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...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class...

    Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class declaration for Time to complete the questions below: DO NOT WRITE MORE THAN ASKED FOR. class Time private: int hours; int minutes; public: Time(); Time (int , int m = 0); void addMin(int m); void addHr(int h); void reset(int h = 0, int m = 0); Time operator+(const Time & t) const; Time operator-(const Time & t) const; Time operator*(double n) const; friend Time...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT