0

Let's say i have three variables:

a, b, c

and i set them such values:

2,1,3

I have such string:

ilovemama

how could i change char position, via block's of three, in my case i have three blocks:

ilo
vem
ama

let's try on first block:

1 2 3
i l o

and i must change this position's via my a,b,c:

2 1 3
l i o

And so over, and then concat this block's into one line... I think that i explain it normally.

I can do this on jQuery, but i can't imagine, how to do this on pure JS. i try a little bit, but this is without sense(

asked Nov 28, 2013 at 17:08
5
  • Is this a homework assignment? Commented Nov 28, 2013 at 17:11
  • @rdodev maybe yes, i know what you would say now, i'm a bad boy, go and read, SO is not for that... But! SO is for helping people, and i want to know how to do this, even if not all code, but some explain i need... I think you don't know every thing on this planet too, and need sometime help... Commented Nov 28, 2013 at 17:13
  • @rdodev did you give me -1? if so;than thank you, i believe life will turn this bumerang to you :) Commented Nov 28, 2013 at 17:21
  • It's not a matter of helping. It's a matter of helping with assignments not being fair to your professor and your peers that you seek answers to your homework here. Commented Nov 28, 2013 at 17:26
  • @rdodev remember, we are people! not robots, you will get bumerang, belive! it's not hard to help, becouse if i could i will not ask, also if i will have some code ideas i will post here, some little help is good, you are angry( where you from? some little help, and i go further, just one stop on begining... it's bad that people are so rude now( you should not be king Crown will not fall... Commented Nov 28, 2013 at 17:29

1 Answer 1

2
var string = 'some string'
 a = string.charAt(0),
 b = string.charAt(1),
 c = string.charAt(2); // and so on
var newString = b + a + c; //oms
var otherString = c + b + a; //mos

.charAt(0) will select the first leter of the string(the one with index 0) and so on. assigning the values to vars you can manipulate the string as I understand you want to do

for blocks, doing;

var string='some string';
var a = string.slice(0, 3),
 b = string.slice(3, 7),
 c = string.slice(7, 11); and so on

Then the same

var newString = c +a +b; // will be = 'ringsome st' 

To find an Index as you request in the comment you can use;

var str = "Hello There",
indexOfr = str.indexOf("r"); 
console.log(indexOfr); // outputs 9

A function could be;

function dynamo(string) {
var len = string.length-1,
 parts = 3,
 whereToCut = Math.floor(len/parts);
var a = string.slice(0, whereToCut), 
 b = string.slice(whereToCut, (whereToCut *2)), 
 c = string.slice((whereToCut *2), len+1);
return b + a + c;
//(or you could hwere some code to see what order you want, i dont understand your request there) 
}
dynamo('what do you really want to do??');
//returns "u really wwhat do yoant to do??"
answered Nov 28, 2013 at 17:14
Sign up to request clarification or add additional context in comments.

8 Comments

thats what you want to accomplish, but of course is just an example and not the actual entire work done with the proper string
as i thing i also need to split by blocks (for example i have string with 6 chars), and then,but! otherString could be other order, and how to be than?
; and so on main trouble is that it must be "dynamic", not hardcode ^)
that is not what you asked, please explain how dynamic needs to be, as the solution would be the same but in a for loop or an event handler or I dont know, dynamic means a lot
see, you hardcode, if i have 3 blocks (9 chars), but what will be if i have 299? string.slice(0, 3), will be not so good )) also c +a +b; is hardcode too, becouse i could have a+c+b etc, according to a,b,c value's, as in question 2,1,3
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.