1

Tried googling for a while but couldn't find my answer. I am trying to take a 3-character string and convert it to a 6-character string in which each of the 3 characters is duplicated.

For example, 3ff would become 33ffff.

First, I'm turning the string 3ff into the array ["3", "f", "f"].

After I converted to an array, I tried to map through the array and *2 each value, hoping to get the new array ["33", "ff", "ff"].

However, I am getting the new array [6, NaN, NaN].

I understand that 3 is getting multiplied by 2 for 6, and that JS doesn't know how to multiply strings so we get NaN.

I thought that in Python, if you try to *2 a string value, it will just duplicate the value, like eggs will turn into eggseggs. Is this not the case in JS?

Here is my code:

const duplicate = function(v) {
  newArray = v.split("");
  doubledArray = newArray.map(i => i*2);
  return doubledArray
};

console.log(duplicate("3ff")) // Output: [6, NaN, NaN]

Is there a way to duplicate string values of an array in JavaScript, and not just number values?

4
  • 4
    No it's not possible to multiply. But you can just do i => i + i instead Commented Mar 28, 2021 at 18:37
  • @Alto JavaScript, where '1'+'1' == '11'. Python, like Ruby, has string-specific overrides for *. JavaScript does not. Commented Mar 28, 2021 at 18:40
  • 1
    "3ff".split('').map(x => x.repeat(2)).join('') Commented Mar 28, 2021 at 18:52
  • Thanks @Alto! Tried this and it works great :) Commented Mar 28, 2021 at 20:34

7 Answers 7

3

You probably want to use .repeat() function like:

const duplicate = (v) => v.split("").map(i => i.repeat(2));
console.log(duplicate("3ff"));

Old notation:

const duplicate = function(v) {
    newArray = v.split("");
    doubledArray = newArray.map(i => i.repeat(2));
    return doubledArray
};
console.log(duplicate("3ff"));

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

1 Comment

I hadn't heard of the repeat function before. Thank you! This worked perfectly.
2

No need to use arrays for this:

console.log('abc'.replace(/./g, '$&$&'))

In regexp language, a dot means "any symbol" and $& = "the found symbol"

1 Comment

Oh cool! I didn't even think that there could be a way to do this without using arrays. This is a good tip I'll keep in mind for the future.
1

This can help you

const duplicate = function(v) {
  return [...v].map( i => i.toString() + i).join('');
};

console.log(duplicate("3ff")) 

1 Comment

Ahh, I see what you did there. Thanks for this suggestion! So many creative ways of doing the same thing :)
1

Try this

const duplicate = function(v) {
    newArray = v.split("");
    doubledArray = newArray.map(i => i + i);
    return doubledArray.join("")
  };

console.log(duplicate("3ff"));

Comments

1

Please follow like this.

doubledArray = newArray.map(i => i+''+i);

Because I may be a number, so you should add string.

4 Comments

string + string is always another string
But if the i is numeric value, it will be calculated.
Ah I see. I was looking at "3ff"
i is not a number. split('') creates array of strings
1

You can use string concatination using string template literal.

const duplicate = function (v) {
  return [...v].map((c) => `${c}${c}`);
};

console.log(duplicate("3ff"));

1 Comment

Makes sense! I would never have thought of this. So useful to see everyone's different thought processes of going through the same problem.
0

Answered! There were many ways to do this, but here were the two simplest for me to understand:

1.) Use repeat, like this:

const duplicate = function(v) {
    newArray = v.split("");
    doubledArray = newArray.map(i => i.repeat(2));
    return doubledArray.join("")
};

console.log(duplicate("3ff")); // Output: 33ffff

2.) Use i + i, like this:

const duplicate = function(v) {
    newArray = v.split("");
    doubledArray = newArray.map(i => i + i);
    return doubledArray.join("")
  };

console.log(duplicate("3ff")); // Output: 33ffff

Thanks all for the great suggestions!

Comments

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.