Javascript Practice Problems: Get Initials

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

In this video, we will look into parsing a Javascript string in order to obtain the initials of that string.

After you've learned the basics of programming and how to program in Javascript, the next steps would be to practice applying what you have learned to solve various programming problems. These problems are the best way to move from a beginner into an expert:

Рекомендации по теме
Комментарии
Автор

He doesn't really explain the code at all. Here is my attempt at explaining what is going on:

1. function getInitials(name) {
2. let initials = ' ';
3. let waitingForSpace = false;
4.
5. for (var i = 0; i < name.length; i++) {
6. if (!waitingForSpace) {
7. initials += name[i];
8. waitingForSpace = true;
9. }
10.
11. if(name[i] === " ") {
12. waitingForSpace = false;
13. }
14. }
15. return initials;
16. }
17. console.log(getInitials("John F Kennedy"));

1. this is simply setting up the function that has been named 'getInitials' with the argument of 'name'. The value of name is set when we call the function on line 17, and pass in the parameters of the argument.

2. setting a variable called 'initials' with the value of an empty string, for now. The goal is to fill this variable with what we want, and then return that variable so we can log it to the console eventually and see the final value.

3. creating a variable with the Boolean value of false. By manipulating this variable, we can control what is actually saved as an initial later on.

5. setting up a loop. setting a variable of i to 0. The value of i will change each time the loop starts over. The loop is saying, "For as long as the value of i is less than the length of the value of 'name', continue the loop. Each time the loop starts over, increase the value of i by one (i++)."

6. This is the beginning of an if statement, where only if the beginning of the statement is true will the rest of that block of code continue, otherwise it will be skipped over to line 11. So, if waitingForSpace is false (!waitingForSpace), then continue on to line 7 and 8. Since we already set it to false on line 3, we will continue on to 7 and 8 for the first round of the loop.

7. this is shorthand for initials = initials + name[i]. Since i is 0 right now, that would be name[0], which is in the example, 'J'. So now, initials = 'J'.

8. we are now changing waitingForSpace to true, which will make line 6 incorrect, and will skip to line 11 for the next iterations of the for loop.

11-12. These are pretty straightforward. If name[i] is equal to the value of a space, than waitingForSpace is changed back to false, and the if statement on line 6 becomes correct again, and the next round of the loop will go through and add the next initial to 'initials'.

15. return 'initials', so that when we try logging the function 'getInitials' to the console, there will be something to log.
17. we are telling the computer to log the function 'getInitials' with the argument of "John F Kennedy" to the console.

Hopefully that was helpful for anyone confused by the video.

josephanderson
Автор

For me: Javascript learner at
intermediate level, the tutorial was great! I'm learning to solve algorithm problems with Javascript, and it was really helpful. If you slowed down it would be a little boring for me. So it always depends on the level of the person watching. One suggestion is just change the name of the video like "Javascript Practice Problems: Get Initials". Anyway, keep it going!

lumacagnan
Автор

PLEASE NEXT TIME ZOOM IN THE CODE I CANT SEE IT I AM ONLY LISTENING AND THANKS ALOT

rahadafoad
Автор

Not sure why this has so many likes or good feedback. You obviously know what you're talking about and seem to be experienced and that's great, but if you're going to make a video for javascript beginners (such as myself), then SLOW DOWN and explain what you're doing!! Rather than saying, "here you can do" (WHICH IS HELPLESS), you can say, "here we are going to do "...." because of . It was pretty cool watching you do this and how quick you were able to, but this did not help a beginner like myself become any better. Please don't assume we understand exactly what you're doing or why you're doing it.

gmoy
Автор

I think the shortest way to do that example is something like this (only 2 lines)
const getInitials = (str) => str.split(' ').map(a => a[0]).join('');
console.log(getInitials('John F Keneddy'));

pjguitar
Автор

I am more confused than i started, you keep typing in staff and not explaining why you??
slow it down

mwenyeji-
Автор

Dude i hope your other videod are more zoomed inside of the editor. Its hard to read on mobile

WomboBraker
Автор

Great tutorial Cody! :) Keep going, this is really great way to learn. PS. Please make font bigger its really hard to see

milanstojevski
Автор

Why i get undefined?
function func(string) {
const array = string.split(" ");
array.map(function(word) {
return word[0];
}).join('');
}

console.log(func("John Doe"));

lulualmohtarif
Автор

For those of you who are complaining about this video: Don't play with coding! Go to watch kitty videos! It's better for your brain!
Cody Seibert, how to deal with chronic complainers: Just ignore them!

tbfifalover