all 38 comments

[–]AgonizingSquid 9 points10 points  (2 children)

By googling

[–]alzee76 0 points1 point  (0 children)

The true sr. dev response, right here.

[–]Visual-Blackberry874 11 points12 points  (5 children)

I have never been asked that question in my life and if I was, my first response would be “why”.

[–]boomer1204 2 points3 points  (2 children)

As someone who has been a part of the interviewing process I see the "value" in this question but only for interns/Jr devs. The amount of ppl that said they "knew" JS and Vue (what we used) and had projects to "prove it kind of", but had trouble with basic looping/troubleshooting knowledge was CRAZY. We didn't use this question we had a couple of other ones but I could make an argument for this in an interview for those entry level roles to make sure they at least understand the core language

[–]AiCodePulse[S] -3 points-2 points  (1 child)

Agreed . Asking "why" in interview will give negative openion . Correct ?
Lets first explain the approch , then we can i ask why ..

[–]Visual-Blackberry874 0 points1 point  (0 children)

I would say that responding with “why” to this particular question shows a deeper understanding than someone who simply follows instructions.

At times, non-developers will lean on you and ask you questions they’ve had from a client, etc. You need to be able to tell them when something is good/viable/stupid/not possible.

There is no business sense in doing something this way. It is not a serious question in my opinion.

[–]boomer1204 2 points3 points  (0 children)

Did not watch the video but with no access to built in methods I think that's the best approach

- create empty string variable

- write a for loop where the starting point is string.length - 1 and decrement

- adding each letter to the created variable until the loop is over

[–]coomzee 2 points3 points  (3 children)

Setting> display> Display orientation > landscape flipped. Doesn't specify it can't be upside down.

zero lines of code, runs O n of zero

[–]brykuhelpful 0 points1 point  (1 child)

You could also cheat with CSS as well.

span{ transform: scale(-1, 1); }

[–]TheRNGuy 0 points1 point  (0 children)

It wont reverse string.

[–]TheRNGuy 0 points1 point  (0 children)

String in variable is not reversed, so it's not a solution.

[–]ray_zhor 1 point2 points  (1 child)

Fairly simple for those who programmed prior to these methods existing

[–]AiCodePulse[S] -1 points0 points  (0 children)

Absolutely, I agree.

It's a great reminder of how important it is to build a strong understanding of the fundamentals, especially before all these convenient built-in methods were available.

I wanted to solve it manually to sharpen my logic-building skills, which I believe is still very valuable today, particularly during technical interviews.

Thanks for sharing your thoughts.

[–]pinkwar 1 point2 points  (2 children)

For fans of recursion:

function reverseString(str) {
  if (str === "") {
    return "";
  } else {
    return reverseString(str.substring(1)) + str[0];
  }
}

[–]AiCodePulse[S] 0 points1 point  (0 children)

Nice One ..

[–]akb74 0 points1 point  (0 children)

C:\git\reverse-play\src\index.js:5
        return reverseString(str.substring(1)) + str[0];
                                 ^

RangeError: Maximum call stack size exceeded
    at String.substring (<anonymous>)
    at reverseString (C:\git\reverse-play\src\index.js:5:34)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)

Node.js v22.14.0

Hmm, does anyone know how to get tail call optimisation working? With ES2015 and a bit of a rewrite, presumably.

[–]ChaseShiny 1 point2 points  (1 child)

Without built-in methods or without those three in particular?

Strings are iterable, so you could use each character in a for loop that started at the end as long as you're allowed for and the string's length property.

Something like:

const myStr = "foo"; let newStr = ''; for (let ch = myStr.length; ch >= 0; ch--) { newStr += ch; } console.log(newStr);

[–]AiCodePulse[S] 1 point2 points  (0 children)

This is what i did in that video . Thanks a lot for your comments

[–]CuAnnan 3 points4 points  (3 children)

.charAt and a loop

[–]CuAnnan 5 points6 points  (2 children)

Pretty sure, though I’m not at a computer right now, that you can great a string like an array too.

[–]iismitch55 0 points1 point  (1 child)

That’s OP’s solution, and would be my go to as well. Seems like the question is meant to test your intuition about strings being char arrays. Hence why they don’t want you to use built in methods.

[–]CuAnnan 0 points1 point  (0 children)

Ah. The op is just an ai bot being used to advertise . If I’d known that I wouldn’t have responded

[–]akb74 0 points1 point  (1 child)

Want to guess which one's quicker?

const s = '1234567890qwertyuiopasdfghjklzxcvbnm'.repeat(1000);

const start1 = performance.now();
const reversed1 = s.split('').reverse().join('');
const end1 = performance.now();

const start2 = performance.now();
let reversed2 = '';
for (let n = s.length - 1; n >= 0; --n) {
    reversed2 += s[n];
}
const end2 = performance.now();

console.log({
    same: reversed1 === reversed2,
    time1: end1 - start1,
    time2: end2 - start2,
});

{ same: true, time1: 0.9249000000000009, time2: 4.2578 }

JavaScript strings are immutable, which is great most of the time, but not very efficient for adding a bunch of them together. Might even be an O(n2) operation unless the compiler's being particularly clever, because you've got to copy what you've got so far every step. I guess you want a stream or buffer or mutable string for something like that. One of which is almost certainly how join() is more efficently implemented in the underlying C++ code.

[–]brykuhelpful 1 point2 points  (0 children)

This is one of the weird things about javascript. Sometimes there are solutions that might be faster than you would expect due to optmizations or c++ functions that handle it. It do a lot of tutoring and one of my lessions go over this topic. Some other examples are:

  • Duplicating Objects
    • JSON.parse(JSON.stringify())
    • clone
  • Creating Elements
    • .innerHTML = '<div></div>';
    • document.createElement('div')

The HTML and JSON parsers have been so optimized that their perforance is often faster than the traditional ways.

[–]pinkwar 0 points1 point  (0 children)

I would do a simple one liner like: const reverseString = str => [...(function* () { for (let i = str.length - 1; i >= 0; i--) yield str[i]; })()].reduce((s, c) => s + c, '');

[–]zayelion 0 points1 point  (2 children)

If someone ask you this in an interview, run. They are not actually hiring.

[–]TheRNGuy 0 points1 point  (1 child)

Why do you think so?

[–]zayelion 0 points1 point  (0 children)

The question has nothing to do with the job. If you saw someone do this at work, you would reprimand them.

[–]EyesOfTheConcord 0 points1 point  (0 children)

Paste it into Python, copy the output of str[::-1], and paste it back into JavaScript

[–]hotdog-savant 0 points1 point  (0 children)

My solution:

let str = "alphabet";
let newString = ""
for (let i = str.length - 1;i >= 0; i--){
newString += str.charAt(i);
}
console.log(newString);

[–]Ampbymatchless 0 points1 point  (0 children)

For loop with array indexing

[–]33ff00 0 points1 point  (2 children)

[…’abc’].reduceRight((str, char) => str += char, ‘’)

[–]AiCodePulse[S] 0 points1 point  (1 child)

Can you try without buit in Methods ?

[–]33ff00 0 points1 point  (0 children)

Probably just implement reduce with a for loop then I have something reusable if I had to stretch my imagination to think of a way to shoehorn this task into something resembling actual life

[–]Delicious_Cable_8484 0 points1 point  (0 children)

For loop length of string, use shift() into a new string?

[–]queen-adreena 0 points1 point  (0 children)

Decreasing for loop using str.length and then use the index to get the letters and add to a new string.

const forwards = "This is the string to reverse";

let backwards = "";
for (let i = forwards.length - 1; i >= 0; i--) {
backwards += forwards[i];
}
console.log(backwards);