“Squares of a Sorted Array” Javascript Solution Cheat Sheet.

The “Squares of a Sorted Array “is in the easy category and is a good start for understanding more challenging algorithms using arrays as a data structure. I will focus on explaining a solution that works and not on the O time and space complexity.

Challenge: “Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.”LeetCode.




loop through the array, square each element then return a sorted array from smaller to greater.

1.Loop through the array

for (i= 0; i< nums.length;i++){}

2. Square each element using Math.pow(base, exponent)

nums[i] = Math.pow(nums[i], 2)

3.Return sorted array using sort method

return nums.sort((a,b) => a - b)




Lifelong learner , Full Stack Software Engineer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store