how to repeat the 123 1231 till the length target

Viewed 64

how to print the number in str and repeat it till the length target, example:

if we have num = 4 and the str target we print is 123,

so the newStr = should be 1231 and if the num = 6 the newStr = 123123

the newStr.length should be like the num

function repeatIt(num) {
  var arr = [],
    markIt = true,
    add = true,
    hastag = true;
  while (arr.length < num) {
    var pushThis = "";
    if (markIt) {
      pushThis = "!";
       markIt = false;
    } else if (add) {
      pushThis = "@";
       add = false;
       hastag = true;
    } else if (hastag) {
      pushThis = "#";
       hastag = false;
       markIt = true;
       add = true;
    };
    arr.push(pushThis)
  };
  return num > 0 ? arr : "invalid input"
};

console.log(repeatIt(3))
// output : ['!','@','#'] 123 

console.log(repeatIt(6));
// output : ['!','@','#','!','@','#'] 123 123

console.log(repeatIt(4))
// output : ['!','@','#','!'] 123 1

console.log(repeatIt(0)) // invalid input

the above is my example and that is my code, I check and change the parameter, it matches with the output I want, but my codes seem not clean and too long, is there any way to write the code better for that example above, ??

3 Answers

You might use an array of characters, then use Array.from to construct the array, and check i % chars.length to get the correct character:

function repeatIt(length) {
  if (length === 0) return "invalid input";
  const chars = ['!', '@', '#'];
  return Array.from(
    { length },
    (_, i) => chars[i % chars.length]
  )
}

console.log(repeatIt(3))
// output : ['!','@','#'] 123 

console.log(repeatIt(6));
// output : ['!','@','#','!','@','#'] 123 123

console.log(repeatIt(4))
// output : ['!','@','#','!'] 123 1

console.log(repeatIt(0)) // invalid input

Your function should really have two arguments: the original string and the target length.

It could look like this:

function repeatIt(str, num) {
    while (str.length < num) {
        str += str; // double the length
    }
    return str.slice(0, num); // chop off 
}

console.log(repeatIt("!@#", 8));

If you are allowed to use the built-in .repeat() method, then:

function repeatIt(str, num) {
    return str.repeat(Math.ceil(num / str.length)).slice(0, num);
}

console.log(repeatIt("!@#", 8));

If input and/or output needs to be an array of characters, you can just apply .split("") to turn a string into an array, and .join("") to turn an array into a string.

If I understand your question correctly, then this can be simplified by using the % modulo operator as follows:

function repeatIt(num) {
  
  if(num <= 0) return;

  // The dictionary and result arrays used to generate result
  const dict = ['!','@','#'];
  const result = [];      
 
  // Iterate over the number range provide
  for(var i = 0; i < num; i++) {
    
    // For each iteration, add item from dict
    // that is current iteration, modulo the length
    // of characters in the dictionary
    result.push( dict[i % dict.length] );
  }
  
  return result;
}

console.log(repeatIt(0));
console.log(repeatIt(3));
console.log(repeatIt(4));
console.log(repeatIt(5));
console.log(repeatIt(10));

Related