Skip to content

Instantly share code, notes, and snippets.

@felipekm
Created July 12, 2022 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipekm/b0cb82dec5cc9dfb46d589ee31fd2da0 to your computer and use it in GitHub Desktop.
Save felipekm/b0cb82dec5cc9dfb46d589ee31fd2da0 to your computer and use it in GitHub Desktop.
JS challenge

JavaScript

Sorted Squared Array

Write a function that takes in a non-empty array of integers that are sorted in ascending order and returns a new array of the same length with the squares of the original integers also sorted in ascending order.

Print the output like this: INPUT [1, 2, 3..] > OUTPUT [1, 4, 9..]

Sample Input

  array = [1, 2, 3, 5, 6, 8, 9]

Sample Output

    [1, 4, 9, 25, 36, 64, 81]

Test Case 1

{
  "array": [1, 2, 3, 5, 6, 8, 9]
}
Output
    [1, 4, 9, 25, 36, 64, 81]

Test Case 2

{
    "array": [-2, -1]
}
Output
    [1, 4]

Test Case 3

  "array": [-10, -5, 0, 5, 10]
Output
    [0, 25, 25, 100, 100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment