Let's now get into the code for the approach that we have discussed in the previous video.
So first we are going to create a new array.
So let me write let new array.
So let's just call it new array itself.
And this new array will have the same size as the input array which is given to us over here okay.
So let new array equal to new.
Array and the length of this is going to be array dot length.
Where array is the input array which is given to us over here.
Okay.
And what we are going to do is over here, we are going to fill the value zero at every index in this
new array.
So I can say dot fill zero okay.
So at this point new array has the same size as the input array.
But every cell in the new array has the value zero.
Now what we are going to do is we are going to iterate through every index in the new array.
And then let's go ahead and square the element in the input array and place it at that particular index.
So I can say for let I is equal to zero.
I less than array dot length I plus plus okay.
And over here I'm going to say new array at index I is equal to array.
At index I into array at index I okay so that's it.
So we have filled the new array with values such that at every index which is squaring the value in
the given input array and placing it in the new array.
But we have seen that this will not work because if the input array has negative and positive numbers,
like for example, if the input array is something like minus four and then you have one, two and three
okay.
So clearly minus four square is greater than one square.
So this will not work.
So what we have to do is after this okay.
So let's have three over here.
So after this we have to go ahead and sort the new array.
So let's do that.
So again say new array dot sort.
And we are going to sort the array in ascending order.
So I can say a comma b and the criteria is a minus b.
So we are sorting the new array in ascending order.
And finally we just need to return the new array.
So I can say return new array okay.
So this should work.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.
Now you can make use of the user logs okay.
So in case your stack somewhere do make use of it to debug your code.
So you can just add console log statements and you can print what is happening at that particular stage
of your code.
And that will help you debug your code.
But over here you can see what is the input for various test cases and what is the expected output and
what is the actual output.
Now you will get over here.
When you go to user logs, you will get these statements even if your code is passing the test case
or it's failing the test case.