• 0 Posts
  • 1 Comment
Joined 2Y ago
cake
Cake day: Apr 19, 2022

help-circle
rss

a couple notes

  • you should declare all variables with let before you assign them, it’s good practice and you can enforce it by enabling strict mode - put "use strict"; at the beginning of your function (or the entire script). Of course it’s only needed in browsers, strict mode is usually enabled by default in most tools.
  • try not to execute extra code if you can help it. For example, in this case only the final reversedWord value matters, so you can do it at the end as opposed to on every iteration. Your code right now works in O(N^2) - with every new character in the string its speed decreases exponentially, but it should work in O(N) - a linear time. If you couldn’t create reversedWord at the end, you could still initialize it with an empty string and append some text with += on every iteration, that still works in O(N) time as you don’t have to recreate the entire string on every iteration.