Repeat a String: Basic Algorithm Scripting Free Code Camp JavaScript

preview_player
Показать описание

Fan funding goes towards buying the equipment necessary to deliver 4k videos, 4k webcam, and a high quality microphone better audio. Any support is very appreciated!

My channel is here for aspiring programmers to learn easier and help debug any issues from the many great free resources available on the web.

Check out my other videos going over HTML, CSS, Ruby, Ruby on Rails, Java, JavaScript, Python, PHP, SQL, Command Line, BootStrap, jQuery, and WordPress from CodeCademy, CodeCombat, FreeCodecamp and more!

-~-~~-~~~-~~-~-
Please watch: "How I Became a Developer | My Developer Journey of the Last 3 Years | Ask a Dev"
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

So cool to have these tutorials while thinking about the exercises!

jasminherself
Автор

Thanks a lot. Was looking for it. Got a follower.

kouIShereNOfear
Автор

I'm plugging your channel in FCC chat :) thx 4 the help

mshim
Автор

Hi, first of all thanks a lot for your videos. You rock. Just want to share with you my solution to this exercise, it's a little bit different but gets the job done:

function repeatStringNumTimes(str, num) {
var newStr = "";
for (var i = 0; i < num; i++) {
newStr += str;
}
str = newStr;
return str;
}

LuisForra
Автор

some funky way could be: function repeat(str, num){return

VKBteamkiller
Автор

function repeat(str, num){return str.repeat(num>0?num:0)}

VKBteamkiller
Автор

Is there a problem with simply concatenating the string num number of times in the for loop?

AwaMelvine
Автор

ES6 Add support for this "str.repeat(count)". But this is a classic question for interviews, and they want to see if you know regarding prototype and what is "this" of prototype context. So must make like that:

String.prototype.myRepeat = function(count) {

var result = "";

for(var i =0; i < count; i++ ) {

result += this + ' ';

}

return result;
}



AND WHEN YOU CHANGE PROTOTYPE, best practice if(! String.prototype.myRepeat) {...code...}

valikonen
Автор

Javascript solution:
function repeatStringNumTimes(str, num) {
// repeat after me

var blankArray = "";
for(var i=0; i<num;i++){

blankArray+=str;
}

return blankArray;
}

repeatStringNumTimes("abc", 3);

thegameguy
Автор

is it wrong to use the built in method string.prototype.repeat()?
I solved the problem like this.

function repeatStringNumTimes(str, num) {
// repeat after me

if(num >= 0){
return str.repeat(num);
}
else{
return "";
}

}

repeatStringNumTimes("abc", 3);

Crisssti
Автор

This worked, 4 and a half years later! :D! Some of these dont work though :( .

arikelvara
Автор

I used this code and it worked:


function repeatStringNumTimes(str, num) {

var blankArr = "";

if (num >= 0) {
return str.repeat(num);
} else {
return blankArr;
}
}

repeatStringNumTimes("abc", -1);



Is the code ethical?
Should I try to solve it through other methods?
Basically, I understand that for every basic algorithm you have a method that you can use (like repeat, push, join, etc.), so you don't need to take the long way to solve it.

Thank for your videos! :)

CristianIoan
Автор

Cheers ~

if (num < 0) {
var invalid = "";
return invalid;
} else {
var finalStr = str.repeat(num);
return finalStr;
}

bawbee
Автор

Thanks for the videos, they've been super helpful so far for someone just learning this. Is there a reason not to use str.repeat(num) with an if statement for negative numbers besides it feeling too easy?

michaelreider
welcome to shbcf.ru