this post was submitted on 29 Aug 2023
17 points (94.7% liked)
General Programming Discussion
7700 readers
1 users here now
A general programming discussion community.
Rules:
- Be civil.
- Please start discussions that spark conversation
Other communities
Systems
Functional Programming
Also related
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
a couple notes
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.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 createreversedWord
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.Try to be precise when teaching others. O(N^2) increases "quadratically" not exponentially. O(k^N) would be exponentially increasing with N.
(For point 1) Got it, from now on variables will be declared with
let
. I don't understand what is"use strict";
maybe you can explain it.(For point 2) I was testing to see if
reversedWord
printed the desired output in the Console, forgot to remove it after finishing the program. I also don't understand what 'O(N^2)' and 'O(N)' is, but+=
-ing an empty string is a great idea, why didn't i think of that."use strict";
is just a way of enabling more restrictions to make writing JS less error prone. You can find the details of what that does on MDN (or, if you don't like long verbose technical explanations, this is a pretty good summary!)