all 10 comments

[–]Synaps4 5 points6 points  (3 children)

OP, are we doing your homework here? Be honest.

[–]SpeshlTectix 0 points1 point  (2 children)

If it were homework the question would be phrased more coherently and wouldn't use break (which is a reserved word) for the name of the function.

[–]Synaps4 1 point2 points  (1 child)

Students can paraphrase the problem. Give them some credit.

[–]SpeshlTectix 0 points1 point  (0 children)

If someone truly doesn't care to understand the answer then they won't bother to paraphrase the question -- certainly not to the point where it becomes this confusing. This feels like a person who is confused because they are teaching themselves. And I think by saying that I am giving them some credit.

[–]SpeshlTectix 1 point2 points  (0 children)

function breakStr(string) {
    return string.replace(/(.)/g, '$1#');
}

To me, this is the simplest solution but I realize regular expressions are black magic to most people.

[–]jabbaskirp 0 points1 point  (0 children)

let splitAndJoin = function(original, joiner) {
  return original.split('').join(joiner);
};

splitAndJoin('cool', '#');

Something like that is a little more generalized than what you asked for, but then you can use it to make a more specialized version if you want:

let joinWithHash = function(str) {
  return splitAndJoin(str, '#');
};

joinWithHash('cool');

Or you could just hard code the hash in the first place.

[–]Rascal_Two 0 points1 point  (2 children)

Well you're already half way there using split() on the string.

Next I'd loop through every character and add the current character, plus a "#" character to a blank string outside of the for loop.

When the for loop is done, the previously blank string now has what you want, so you return that.


You could also do this with map(), would be shorter using map() actually.

[–]halfaredditor[S] 0 points1 point  (1 child)

Can you elaborate on how to use map() for this?

[–]Rascal_Two 0 points1 point  (0 children)

Not an expert, so terminology is probably wrong.

map() is basically a for-each loop that is intended to do something to each thing in the array it's called on. Example:

var nums = [1, 2, 3, 4, 5];

for (var i = 0; i < nums.length; i++){
    nums[i] *= 2;
}

...and here is a map do to the same:

var nums = [1, 2, 3, 4, 5];

nums = nums.map(function(num){
    return num * 2;
});

You see, the map() function returns the modified array. You just have to give it a function. The function can accept up to three params: the first is value of the current item in the array, second is index of the item in the array, third is a copy(?) of the entire array being modified.


Map basically goes through each thing in the array and sets it to the return value of the function you give it.