Answer the question
In order to leave comments, you need to log in
How to reverse a string without using standard methods?
Tell me how to reverse a string without using standard methods, for example, reverse(), etc. Also, you do not need to use an array, additional variables. You just need to change the value of the string on the spot.
For example, an additional variable o is used here;
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
}
function reverse(s) {
for (var i = s.length - 1; i >= 0; i--) {
s+= s[i];
}
return s.substring(s.length/2);
}
Answer the question
In order to leave comments, you need to log in
You just need to change the value of the string on the spot
var str = 'abc'
str[1] = '!'
console.log(str) // чё, ждали "a!c"? - а вот ни хрена, "abc"
let str = 'abcdefg';
for (let i = 0; i < str.length; i ++) {
str = str.slice(1, str.length - i) + str[0] + str.substr(str.length - i);
}
console.log(str);
str.split('').reverse().join('');
UPD: suitable options:
https://jsfiddle.net/1mcLfbdh/
https://jsfiddle.net/tfyfdbch/
unsuitable:
let x = [];
'Hello world!'.split('').forEach(s => x = [s].concat(x))
console.log(x.join(''))
https://jsfiddle.net/e2x7c463/let x = '', i = 'hakuna Matata!'.split('')
while (x+= i.splice(-1, 1), i.length) {}
x+=i
console.log(x)
https://jsfiddle.net/dc9fkdot/Does recursion work for you?
function func(str) {
if(str === '') {
return '';
}
return func(str.substr(1)) + str.charAt(0);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question