-4

I'm making a function that takes a string, cuts the first half (leaving middle character if odd string.length) and adds first half to end of string.

For some reason my function only partlyworks: it adds the substr to the end but doesn't cut it from the start. I tried .replace but not working.

What am I doing wrong? And/or is there a better way?my function..

Tam Borine
  • 1,474
  • 2
  • 13
  • 21

2 Answers2

2

replace returns a new string with the replacement, it doesn't modify the string you call it on.

Additionally, as Pointy pointed out, you've passed the literal string 'substr' in, rather than passing in the variable substr.

So:

s = s.replace(substr, '');
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

a friend just gave another way to write a function that does what I wanted mine to do . I'm an amoeba and you're all wizards

function doit(s){
split = s.length /2;

if(split % 2 !== 0) { split = split-1; }
var partOne = s.slice(0, split);
var partTwo = s.slice(split + 1, s.length);

return partTwo + partOne;

}



alert(doit('123456789qwertyuio'));
Tam Borine
  • 1,474
  • 2
  • 13
  • 21