Tell us what’s happening:
I am stuck on how to repeat str. I have it * num right now, but I’m 98% sure that’s not right. However, I am unsure what operation to use to repeat. I thought about a method but that seems overkill and also inappropriate.
I would appreciate your direction and comments. Thank you.
Your code so far
function repeatStringNumTimes(str, num) {
var var1;
var var2;
var i;
for(i=num;i<=str.length;num++){
str = var1, var2;
if(var2%var2 === 2){
return str*num;
} else{
return "";
}
}
return str;
}
repeatStringNumTimes("abc", 3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/repeat-a-string-repeat-a-string
Firstly, you need a method for testing small ideas or code snippets. The Chrome developer tools console is good for that.
If you type something like 'hello' * 3
you’ll see you get NaN
.
So simple multiplication on strings doesn’t work.
‘Addition’ does work, although it’s called concatenation when applied to strings, i. e. it sticks them together.
So think of a method that uses num
as a counter and concatenate str
to itself num
times.
I have my effort to make num a counter and concatenate str to itself num times. It doesn’t work, but please let me know if I’m on the right track with my approach.
function repeatStringNumTimes(str, num) {
//if num is negative return empty string
if(num<0){
return "";
}
//create container for concatenation & create empty array for storage of string as array
var strOne=str.split(" ");
//loop through str using num as counter
for(var i=num;str[num]>=i;i++){
//Below I hope that I am putting num outside of parameter so that it will be imposed on the contents of the parameter but this is not what is happening. Do I have the right idea, though?
strOne == Math.add(str+=str), num;
}
console.log(strOne);
return strOne;
}
repeatStringNumTimes("abc", 3);
}
repeatStringNumTimes(“abc”, 3);
To repeat your string, you could try pushing str
into an empty array num
times, and then using the join()
method to change the array into a string. To be honest, your method seems a bit messy.
if num< 0
return ""
push a str
to an empty array num
times
return [].join('')
join() - to concatenate array items
for - take careful not of how to set the start and end of the loop
I applied the suggestions (I hope) in the correct way, but I am no farther than before.
I feel as if I am eliding some small detail of the code. The suggestion given by @alkapwn3d seems quite reasonable, but I am not sure I am understanding it properly because of the ineffectiveness of the code you see below.
Another worry is that I am not assigning content to variables in the appropriate manner. This is a consistent default of mine and I am attempting to remedy this.
Is variable assignment one of the roots of my problem?
Am I doing too much in one section or stopping short of a secure solution to the challenge?
Your code so far
function repeatStringNumTimes(str, num) {
//create empty array to hold result and initialize arr1 to contain method performed on result array
var result = [];
var arr;
//return empty string if not a positive number
if(num<0){
return "";
}
//loop str num times
for(var num = 0;str>num;num++){
if(num>0){
//push str repeated num times onto result array assigned to resultOne container
var resultOne = result[str].push(num);
console.log(resultOne);
//convert array into str and assign to container arr1
arr = resultOne.join(" ");
console.log(arr1);
}
return arr;
}
}
repeatStringNumTimes("abc", 3);
you should sort out your loop first
this is what your loop is saying:
for
variable num
starting at 0 ;
ending when string greater than num
;
adding the 1 to num
each loop
it should be saying:
for
some new variable starting at 0
ending when new variable is less than the length of string
adding the 1 to the new variable with each loop
next…
you already have a an empty array called result
. now each time for
does a loop, push your string to your empty array.
make sure youre using .push() correctly
you can make new variables but you can also reassign old ones
eg. result = result.join('')
try not to make new variables unless you really have to. every unnecessary code will make your code harder to read and more likely for bugs to occur. remember to keep your code dirt nasty simple
at the end of your function, return what ever you have in result
bonus tip
if you were to read each line as if it was plain English would your code still make sense?
1 Like
Thanks for the feedback. I think this is pretty clear and I’ll keep playing around until I get it.