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(
-
Is this a homework assignment?rdodev– rdodev2013年11月28日 17:11:10 +00:00Commented 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...byCoder– byCoder2013年11月28日 17:13:19 +00:00Commented 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 :)byCoder– byCoder2013年11月28日 17:21:37 +00:00Commented 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.rdodev– rdodev2013年11月28日 17:26:38 +00:00Commented 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...byCoder– byCoder2013年11月28日 17:29:55 +00:00Commented Nov 28, 2013 at 17:29
1 Answer 1
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??"