tried to repeating the double letter of any string.ex- "hello" --> 'hheelloo' but stucked at for loop, any solution?

Viewed 127

function doubleChar(str) {
    var splt = str.split("");

    for(var i=0;i<splt.length-1;i++){
        var rpt= splt[i].repeat(2);
        return rpt.join('');
    }
}

console.log("hello");

8 Answers

An Haskellesque approach here just for fun...

var str    = "hello",
    double = ([c,...cs]) => c ? cs.length ? c + c + double(cs) : c + c : "";
console.log(double(str));

There were few problems in your code :

function doubleChar(str) {
    var splt = str.split("");

    for(var i=0;i<splt.length-1;i++){
        var rpt= splt[i].repeat(2);
        return rpt.join('');
    }
}

console.log("hello");

first is that the code prints string "Hello" no matter what function doubleChar does.

in order to make console.log print result of doubleChar function you need to change it to this

console.log(doubleChar("Hello"))

now your code would call doubleChar function with the string "Hello" as input of function doubleChar and print the output of the function.

Now in your function, the return should not be inside of the loop, when the return is inside the loop, function sends the result of first iteration and there would be no other iterations

function doubleChar(str) {
    var splt = str.split("");

    for(var i=0;i<splt.length-1;i++){
        var rpt= splt[i].repeat(2);
    }
      return rpt.join('');
}

console.log(doubleChar("hello"));

.join('') is for arrays when you want to mix all elements into a string but rpt is not an array but a string so you can just return rpt itself :

function doubleChar(str) {
    var splt = str.split("");

    for(var i=0;i<splt.length-1;i++){
        var rpt= splt[i].repeat(2);
    }
      return rpt
}

console.log(doubleChar("hello"));

you also should not use var inside the loop because by doing so the javascript redefines the rpt and all previous values are lost so you can declare variable outside the loop and append new values inside the loop

function doubleChar(str) {
    var splt = str.split("");
    var rpt=''
    for(var i=0;i<splt.length-1;i++){
        rpt += splt[i].repeat(2);
    }
      return rpt
}

console.log(doubleChar("hello"));

now as final fix, inside your loop you're using i<splt.length-1 this probably because length is one more than maximum index but considering that you are using < instead of <= this is already taken care of. so your final code looks like :

function doubleChar(str) {
    var splt = str.split("");
    var rpt=''
    for(var i=0;i<splt.length;i++){
        rpt += splt[i].repeat(2);
    }
      return rpt
}

console.log(doubleChar("hello"));
var rpt= splt[i].repeat(2);
return rpt.join('');

It is choking on this line because rpt is not an array and you're calling join() on it. Instead, just replace the original array item with the new double item.

function doubleChar(str) {
    var splt = str.split("");

    for(var i=0;i<splt.length-1;i++){
        splt[i]= splt[i].repeat(2);
        
    }
   return splt.join('');
}

console.log(doubleChar("hello"));

all in one line...

str = "hello";
var dbl = str.split("").map(el => el.repeat(2)).join("")
console.log(dbl)

Now here is the same thing, but like your example, we'll only double up single letters

str = "hello", lastletter='';
    var dbl = str.split("").map(el => {
       if (el != lastletter) {
          el = el.repeat(2)
          lastletter = el.split("")[0]
          return el
       }
      }).join("")
    console.log(dbl)

The big problem I see here is that rpt is within the loop. So, the for loop will return "hh", then "ee", so on so forth. In practice, the function stops after returning a value so this would currently return 'hh'. To solve this, you need to move the variable outside of the for loop. Something like this:

var result = ""
for(var i=0;i<splt.length-1;i++){
    result = result + splt[i] + splt[i]
}
return result;

Then you could console.log the result.

The value of rpt is a string, and does not have a join function. You also immediately return inside the for loop, which will do that in the first iteration.

You can split, map and join instead:

const re = "hello".split('').map(s => s + s).join('');
console.log(re);

Answers above/below a great and correct. However, I'd like to answer using almost exactly the same code as in the question as it may be more helpful if you've just started learning JS.

You get "join is not a function" error because .repeat(n) returns a string and strings do not have such a method.

Then, you need to stop return inside of a loop as it actually returns from the whole function so you will only get the first letter doubled as a result. Instead, declare variable for storing result and concat it with a new doubled letter in loop. Return result in the end.

Here is the modified code:

function doubleChar(str) {
    var splt = str.split("");
    var result = ""; // add the var for future result
    
    for(var i=0; i<splt.length; i++){
        var rpt = splt[i].repeat(2); // this is string!
        result += rpt; // concat string to current result
    }
    
    return result;
}

console.log(doubleChar("hello"));

Your declaration of rpt is should be on the top, and the return should be outside your for loop. Other than that, its fine.

function doubleChar(str) {
  var splt = str.split("");
  var rpt = "";

  for (var i = 0; i < splt.length; i++) {
    rpt = rpt + splt[i].repeat(2);
  }
  return rpt;
}

console.log(doubleChar("hello"));

I think most of the solutions here is hello->hheelllloo but you wanted like -> hheelloo So assuming you only want to repeat single char. I'm giving the solution which also is the same code that you gave.

function doubleChar(str) {
    var splt = str.split("");
        var rpt="";
    for(var i=0;i<splt.length;i++)
        rpt+=((splt[i+1]&&splt[i]==splt[i+1]?splt[i++]:splt[i]).repeat(2));
    return rpt;
}

console.log(doubleChar("hello"));

May be the line inside for loop seems complex but if you break this, it will be very simple (splt[i+1]&&splt[i]==splt[i+1]) here I'm checking if the next char exist and equal to the current char, if so then take the current and skip the next char else take the current and don't skip the next char, and after this just repeating the char which we took. rpt+=((splt[i+1]&&splt[i]==splt[i+1]?splt[i++]:splt[i]).repeat(2)); Hope you got this

Related