I'm getting really discouraged with loops

I’ve been stuck for a week trying to really grasp how to use loops, I’m currently trying to do a random password generator project on Scrimba and I can’t complete it.

The project gives me an array with 91 characters, numbers and symbols and wants me to create a password on a button click. it also wants the password to be 15 characters long

the ridiculous thing is that I could probably complete this project without using a loop by creating random variables like this —

let randomCharacter = Math.floor(Math.random() * array.length)
let randomCharacterTwo = Math.floor(Math.random() * array.length)
let randomCharacterThree = Math.floor(Math.random() * array.length)

and I’d basically create another variable that is just adding these up 15 times to create a 15 character random password. I’ve tried this and it worked.

I know this is not the correct way to do this project and I know it’s calling for me to use loops but I just simply can’t grasp them.

I get how to create loops and I get the syntax of them, but I don’t understand how to actually use them. I’ve spent so much time stuck on this and I’m really getting discouraged.

does anyone have a source that actually really helped them with loops?

Can you share your code with us? We might be able to help you fix it and at the same time help you understand loops better.

To display your code in here you need to wrap it in triple back ticks. On a line by itself type three back ticks. Then on the first line below the three back ticks paste in your code. Then below your code on a new line type three more back ticks. The back tick on my keyboard is in the upper left just above the Tab key and below the Esc key.

You’ve got this my friend!!

I haven’t really written any code because I don’t even know where to start regarding loops.

Well, we don’t really write code for people in here, so you are going to have to make an honest attempt. Looping is one of the most basic operations of any programming language. I would find it hard to believe you haven’t been introduced to it yet. Can you at least try something and then let us see it? What type of loop are you trying to use?

I know how to write a loop and I know how to loop through an array.

but I don’t understand how to use them for real world problems. I can log out an array using a loop no problem.

But I can’t use a loop for grab random things from an index for example.

I don’t know what this really means. You need to show us with code. At least paste in what you have so we can have an idea of what you are trying to do.

It takes some time to understand loops, just keep at it. It helps to write a, for example, for loop on a piece of paper and try to understand step by step what it’s doing.

Hey! so loops huh? Almost all of developers i know, who have actually tried to learn to program have gotten stuck at a particular topic at one point or another. for me it was functions! i couldn’t really understand why someone might want to use them when i can do the same thing just by writing code imperatively.

To be honest i kinda gave up on programming for while after that and didn’t start again for 6 months. As to how i got over my fear? i basically kept making up excuses to use them whenever i could. after enough typing and problem solving they just didn’t seem that intimidating anymore.

as for the problem you mentioned, loops solve this exact problem

so instead of using 15 variables to store fifteen different values that you will get from solving the same expression 15 times, you add a label before the code to tell the compiler how many times you want to run a particular block of code.

for example:

If i wanted to, i can print my name on the console 5 times likes this.

console.log("taran")
console.log("taran")
console.log("taran")
console.log("taran")
console.log("taran")

instead, wouldn’t it be much nicer if you had the ability to say something like:

repeat this 5 times:
    console.log("taran")

looks cleaner right?

and this isn’t even the best part yet because sometimes we don’t actually know how many times something needs to be repeated, so we can code something like:

while (some condition is true):
    repead this.

Now that we know how they can help you write cleaner code, lets look at their syntax in a way that would help you think about solving the problem. So whenever i am solving a problem, i often like to talk to myself about the problem to try to better understand what i am actually attempting to solve.

for instance, if the problem is

if given an input array consisting of integers, return an array with each element doubled.

Input: [1, 2,3 ,4 , 5]
output: [2, 4, 6, 8, 10]

to solve this particular problem, i would say to myself something like:

for ( each item in  array){
    1. double the item
    2. add item to result array
}

if you pay attention to the syntax of the for loop, it is pretty much this:

for ([number of times]):
    repeat this

where number of times is a placeholder. you basically tell it the starting position, the ending position and how you intend to iterate (go from one position to the next);

which is why you type:


//     start   end   iterator
for(let i = 0; i < 10 ;  i++)

i hope i was able to help even a bit! let me know if you have any questions! :smile:

1 Like

Wow this answer was incredible, thank you! and since BBsmooth wanted to see my code at the moment. This is where I’m at

const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];

let randomCharacter = Math.floor(Math.random() * characters.length )



for (let i = 0; i < characters.length; i += 1) {
     console.log(randomCharacter[i])
}
let randomCharacter = Math.floor(Math.random() * characters.length )

This is grabbing a single string of length 1 out of the array…

So this loop won’t do whatever you are trying to do.

In general the mismatch between i < characters.length and randomCharacter[i] is probably a red flag.

1 Like

this isn’t actually that far away from what the answer is you’ve used the loop correctly but think about but you want to do inside the loop! so the problem is to create a password by using characters at random indexes from the array

take an empty string variable.

take a character from a random position in the array and add it to the string.

repeat this 15 times.

this is basically what the algorithm is, now try translating these instructions into code.

1 Like

“take a character from a random position in the array and add it to the string.”

this is the part that really throws me off. I don’t understand how to pull something random from the array. I know how to target specific things with their index and I know how to use math.random/floor but I just can’t grasp loops :frowning: I have never been this stuck in my learning.

if you think about it, you use an index to access a particular element of the array right? which in turn is a number and you already know how to generate a random number so you could do something like:

let randomIndex = Math.floor(Math.random() * arr.length);
const randomCharacter = arr[randomIndex]
2 Likes
const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];

let randomCharacter = Math.floor(Math.random() * characters.length )
let randomChooser = characters[randomCharacter]


for (let i = 0; i < 15; i += 1) {
     console.log(randomChooser[i])
     
} 

I really felt like I was onto something but I’m still not getting the results I want hahahaha. now it’s spitting out a random character (which is nice) but then just 14 undefined. I really appreciate you taking the time to reply with such great answers.

The backticks go on separate lines before and after your block of code.

1 Like

This is a single character

Will this single character have 15 different things inside of it?

1 Like

well that is because you are trying to access a primitive’s index.

the randomChooser variable is just a character

what you are basically typing is this,

// get a random index
let randomCharacter = Math.floor(Math.random() * characters.length )
  
// access the element on that index from the array.
let randomChooser = characters[randomCharacter]

//do this 15 times:
   // log the element which is on the i'th index
  // of the character?????
     console.log(randomChooser[i]) 

1 Like
const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];

let randomCharacter = Math.floor(Math.random() * characters.length )

for (let i = 0; i < 14; i += 1) {
     console.log(characters[randomCharacter])
     
}

just keeping you guys updated, I’m still cracking this. I feel like once I crack this it’s going to make loops a lot easier to understand. This is really good practice.

So now I’m getting the same random character repeated for the amount of times to the loop runs which is expected because you guys are telling me it’s just a single character. I understand that the loop isn’t going to produce random characters each time it runs and it’s just going to spit out that 1 random character 15 times. Now I’m just trying to figure out how to make the loop return 15 different characters.

I’m almost there! lol!

const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];



for (let i = 0; i < 14; i += 1) {
    let randomCharacter = Math.floor(Math.random() * characters.length )
     console.log(characters[randomCharacter])
     
}

I think I got it! It was simply putting the variable inside of the loop? So now it’s creating a new character every time the loop runs instead of just once outside of the loop?

1 Like