Basic Algorithm Scripting - Repeat a String Repeat a String

Please help with some direction; struggling with linked challenge…
Here is my work …

function repeatStringNumTimes(str, num) {
  for(let i = 0; i <= num ; i += 1){
    if(num && i <= num){
      str = str+str
    if(num % 2 != 0){
      str = ""
      return str;
  return str;

(repeatStringNumTimes("abc", 3)

my most recent revision…

function repeatStringNumTimes(str, num) {
  let strArr = [];

  for(let i = 1; i <= num   ; i += 1){
    str = strArr.join("")

  if(num % 2 != 0){
    str = ""
    return str
  return str;

console.log(repeatStringNumTimes("abc", 3))

I understand why my original attempt was working… exponentially … against the goal haha :rofl:
my most recent attempt seems to still replicate the data in the last array position improperly or something…

two things:
why do you need this? Do you need this at all:

and why did you place the below inside the loop?

Are you sure you want to change the value of the original string you are using to add the repeats? If you can’t see what this is doing then add console.log(str) after the second line and then repeatStringNumTimes("a", 10) below the function.

i thought i needed that to syphon out the odd number strings… it says return an empty string for odd numbers

Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method.

not positive wow
i see it

i can feel this reply holding the answer to my question… can someone… i dunno, rephrase it??? pleease?

Did you try adding the console.log like I suggested and then calling the function as I suggested? That will show you what is happening to the variable str each time through the loop and will hopefully allow you to realize what is going on.

But if you don’t want to do that then just try a real world example on paper. Let’s call the function as follows:

repeatStringNumTimes("a", 3);

This is the main part of the function:

The first time through the loop (i = 1) we do the following:

strArr.push('a'); // This makes strArr = ['a']
str = strArr.join(""); // This sets str to "a"

So far so good. Now the second time through the loop (i = 2) we do the following:

strArr.push('a'); // This makes strArr = ['a', 'a']
str = strArr.join(""); // This sets str to "aa"

Again, so far so good. Now the third time through the loop (i = 3), I’ll let you fill in the values. Remember, at this point, str = "aa".

strArr.push(str); // What is the value we are pushing here?
                  // What will the value of strArr be?
str = strArr.join(""); // What is the value of str?
1 Like

thank you thank you thank you

i made a str2… it worked… iget it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.