1

How to reverse string using next conditions:

1) if string isn't parameter of function,

2) if string is place in global scope,

3) use loop like for, while,

4) can add one variable

Note: Can't use method .join, .split, .reverse and so on...
If it possible and not so hard for you make explain your solution, very grateful !


In other words, this is what you got:

 let s = 'any string';
 let p; // feel free to use at your discretion

 // your code here. No further variables, no functions and no built-ins
 // should console.log the reversed string

I'm understand that my solution is very close to my desire (conditions), but i can't generate other solution.

function convers(s){ //parameter 
	var str = "";//empty string for new converted string
	for(var i = s.length-1; i >= 0; i--)//loop decremebtation that count from end of string
	str+=s[i];//store result and assignments to str
	return str;// declare result 
}
console.log(convers("abcdef"));

I looked this source: javascript reverse string algorithm

Is there a faster Reverse String Algorithm for JavaScript? - but it is useless for me, sorry.

I'm sorry if my explanation is not clear enough. Sorry for my English, I'm beginner here :))))

8
  • but i can't generate other solution. What does this mean? Which part isn't working? Commented Apr 5, 2018 at 12:49
  • Why do you think the answer can be improved? Commented Apr 5, 2018 at 12:50
  • @gurvinder I think by solution he means algorithm Commented Apr 5, 2018 at 12:51
  • 1
    This smells of do my homework because I do not know what to do. Commented Apr 5, 2018 at 12:58
  • 2
    This looks like a surprisingly good homework/interview question because of conditions 1 (which sorts out recursions) and especially 4 Commented Apr 5, 2018 at 13:07

2 Answers 2

3

You could use a new variable for the reverted string and use the length property for iterating.

var string = 'abcdef',
    reverse = '';

while (reverse.length !== string.length) {
    reverse += string[string.length - 1 - reverse.length];
}
string = reverse;

console.log(string);

A bit shorter.

var string = 'abcdef',
    reverse = '';

while (reverse.length !== string.length) {
    reverse = string[reverse.length] + reverse;
}
string = reverse;

console.log(string);

Sign up to request clarification or add additional context in comments.

Comments

0

Please check below condition I hope it will help you

1) if string isn't parameter of function

function convers(s){ //parameter 
s = s.toString();
	var str = "";//empty string for new converted string
	for(var i = s.length-1; i >= 0; i--)//loop decremebtation that count from end of string
	str+=s[i];//store result and assignments to str
	return str;// declare result 
}
console.log(convers(132));

2) if string is place in global scope

function convers(){ //parameter 

	var str = "";//empty string for new converted string
	for(var i = dd.length-1; i >= 0; i--)//loop decremebtation that count from end of string
	str+=dd[i];//store result and assignments to str
	return str;// declare result 
}
var dd ="123";
console.log(convers());

3) can add one variable

function convers(s,dd){ //parameter 
	var str = "";//empty string for new converted string
	for(var i = s.length-1; i >= 0; i--)//loop decremebtation that count from end of string
	str+=s[i];//store result and assignments to str
	return str +dd;// declare result 
}
    function CallConvers(){
    var dd = "ADD";
    console.log(convers("abcdef",dd));
    }
    CallConvers();

4 Comments

3) can add one variable - you've got two (str and i)
please check now
Good third solutions, but if possible to declare just one variable it can be either global or local scope. Really I don't understand this solution, I deleted second parameter and nothing changed.
@MaximGordiyenko Please check 3rd solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.