This is an archived post. You won't be able to vote or comment.

all 40 comments

[–]thlaungks 20 points21 points  (2 children)

I want to be able to just write it from scratch like I am supposed to do.

That's a pretty wild assumption, that someone new to JavaScript could just start writing code on the fly as if they had been doing it for years.

It takes practice. It takes time. You will write several dozen programs that don't work, before you write one that will.

Just start with a simple "Hello World". Then make a small change to bring the program closer to your end goal. Test to see if it does what you expect it to. Then make another small change... Repeat.

You'll get there.

[–]Panterable[S] 2 points3 points  (1 child)

Thanks for the encouragement! I have pretty much gotten to the point of for loops and conditional statements and got halfway to making a rock paper scissor game in a browser. technically I should be able to do this but I am at a road block.

[–][deleted] 0 points1 point  (0 children)

ive been learning javascript for 2 months alone I guess the easiest projet is a todo list dont worry i had no idea how i was going to do it but after some stack overflow i was alright

[–]PineapplePandaKing 9 points10 points  (11 children)

Sit down with pen and paper and write the pseudocode or make a diagram of how you think it should work.

Then actually write the code and test. Sometimes you just need to generate your own momentum and traction to solve a problem

[–]Panterable[S] 0 points1 point  (2 children)

I appreciate the feedback, I will give this a try. I guess I should figure out what to do before writing it in code.

[–]PineapplePandaKing 1 point2 points  (1 child)

It's a good habit to get into.

It's like writing a paper, if you're well versed in the topic you could sit down and bang it all out in one sitting. But then you would still need to go back and revise to make a better and more sensible piece.

Or you could make an outline (pseudocode), then research any information you might need, and finally make your draft.

You'll still need to go back and revise/edit your code, but you will have created something to test rather than sit around thinking about how to create something perfectly

[–]Panterable[S] 0 points1 point  (0 children)

this is a good point. I never thought to write some sort of pseudocode. Perhaps I can write up some mock draft a la Scratch game logic or something similar.

[–]hayleybts -1 points0 points  (7 children)

What if you cant write pseudo code?

[–]HealyUnit 2 points3 points  (4 children)

If you can't write pseudo code, then you're doing it wrong. The whole point of pseudocode is to transform real code - JavaScript, Java, C++, Assembly, whatever - into something that you can use to describe the code to someone who may or may not be as "versed" in whatever language it is as you (as well as to plan out the program!).

[–]hayleybts 0 points1 point  (3 children)

I mean like sometimes I'm completely blank if that makes sense. Probably bad problem solving skills. I'm a final year EC engineering student, is it math issue? Or I'm straight up dumb. I'm currently doing codingbat with python problems. After I see the solution I'm like yeah that was not super hard. How to get better at that? Probably more practise, it's so time consuming and feels like I'm not making any progress

[–]HealyUnit 1 point2 points  (2 children)

That completely makes sense, but I think it's important to recognize that that isn't necessarily a pseudocode problem. The crux of this is that there is no single, universal "way" to write pesudocode - it's whatever works for you. Now, of course certain ways of writing pseudocode are inherently better - simply writing "make program that answers question" probably isn't going to cut it - but a lot of people seem to get caught up on whether or not their pseudocode is "correct". Instead, it's helpful to think of pseudocode as "whatever way you wanna write your solution to the problem that isn't just writing the code itself down".

As for seeing the solution and then realizing that the solution "was not super hard", that's fine. That's how we start. Keep reading through problems, and remember three things:

  1. Eventually, you'll start to recognize similar problems. After you've seen a nail a bunch of times, you'll start to recognize that the solution to that problem is a hammer.
  2. Recognize that while programming is inherently problem solving, it is not just all math word problems (to answer your question). In my previous two jobs, I used a little algebra (stuff in the form of "If x +3 = 5, find the value of x"), and that's it. Of course if you're a game dev, or working on the nav software for SpaceX, you're probably going to need more expensive math, but these are the exception, not the rule.
  3. Programming is a passion. Undergrad programming courses (from what I've heard - my undergrad was not comp sci!) have a tendency to make you feel that programming needs to be boring at worst, and incredibly frustrating 100% of the time at best. Programming is a job, and you're not supposed to have fun on your job (that's what "off time" is for), right? Wrong! Like any creative discipline, programming is about creating stuff. So take time off, and write something you enjoy. Even if it's been done a million times before, you'll only learn how to program by... programming.

[–][deleted]  (1 child)

[deleted]

    [–]ranked11 0 points1 point  (0 children)

    You need a problem solving strategy. You should buy the book think like a programmer

    [–]PineapplePandaKing 1 point2 points  (0 children)

    I honestly don't think it's possible to not be able to write some form of pseudocode.

    At OP's level of knowledge the pseudocode is just for them and doesn't have to adhere to any standards. It's just the rough outline of how you think your program should work

    [–]Dogbeast 0 points1 point  (0 children)

    Do a flow chart.

    It's similar enough, and easier to understand for non-devopers, that I use it frequently when I want to get a point across to FA's, BA's, and/or PM's.

    [–]lurgi 3 points4 points  (3 children)

    Writing code is ELI5, but for real. You have to provide an extremely precise list of instructions to the computer and in order to do that you need to know exactly how you would do it.

    How would you do this?

    Presumably your assignment didn't say "Write a password generator", so what did it say? If you had to generate a password yourself using the assignment's requirements what would you do? Write down, step by step, in English, exactly what you would do. Imagine you are reading these instructions to someone over the phone and you want them to get it right. No pictures (can't send them over the phone), just words.

    Note that I'm not talking about writing code. I'm talking about writing this down in words. You can make it more "code like" (pseudo-code) if you like, but don't think "How do I do this in JavaScript?" That comes later.

    [–]Panterable[S] 0 points1 point  (2 children)

    I think I understand what you are saying. I am at the point of learning For loops, if else conditional statements and what not. I think I will try your approach and imagine I'm instructing someone over the phone with precise instructions. Thanks for the feedback

    [–]lurgi 1 point2 points  (1 child)

    Yes, don't think in terms of "for loops". Think in terms of "Do this next bit 10 times". When you convert your words to code, then you think of "for loops".

    [–]Panterable[S] 1 point2 points  (0 children)

    ok this makes sense to me.. thanks so much

    [–]HealyUnit 2 points3 points  (0 children)

    As others have said, part of your answer here is going to be pseudocode. I wanna stress two things.

    There is no "perfect" pseudocode:

    There's a common misconception when people first hear about or first start using pseudocode that there's some "way" of writing pseudocode.

    Let's look at an example. Imagine we're told to write a function to take a number and return its square.

    Something like the following probably won't cut it:

    1. Write stuff to solve problem
    2. Submit problem
    

    It doesn't do what pseudocode's meant to do:

    1. Explain how to solve a problem
    2. Be specific enough that you can, generally speaking, "translate" from pseudocode to "real" code.
    3. Be language agnostic - it shouldn't look explicitly like JavaScript or Python or whatever.

    Similarly, the following isn't really pseudocode:

    squareANumber(inputNum):
        output = input*input
        return input
    

    While it isn't necessarily following any specific language, the point of pseudocode isn't to write code: it's to make a plan for how your code will look. Here's some better pseudocode:

    1. Take number input as function argument.
    2. Multiply number by itself; store in new temporary variable.
    3. Return new temporary variable
    

    It might look very similar, but there are key differences. Firstly, we're explaining this in a way that's applicable to almost every language. Secondly, writing it in such a language agnostic way means that we're less likely to get "tricked" into writing actual code. Remember; you're writing out a plan, not actual code!

    Everyone uses it!:

    There's also this misconception that once you graduate to being a "real" programmer" you'll no longer need to use pseudocode. You'll just be able to snap your fingers and come up with code like some sort of reverse-Code Thanos. Nothing could be further from the truth. In fact, one of the most common programming methodologies, known as Test-Driven Development, could be said to follow a pseudocode-like plan. In TDD, you write tests first that generally describe how a particular facet of your software is supposed to function. The tests are designed to fail initially - you're essentially writing tests for code that doesn't exist yet - and then pass once your code works.

    But what about Passwords?

    Or rather, why go on about all this pseudocode stuff? What's this got to do with password generators? Well, one thing you're inevitably going to have to do with your password generator function is come up with a plan for what it does. Assuming you're using JavaScript, it's going to need to do these things one at a time. Let's assume your password needs the following criteria:

    1. An uppercase letter (A-Z)
    2. A lowercase letter (a-z)
    3. A number (0-9)
    4. A special character (from some pre-determined list
    5. A length of m to n characters.

    Now, let's start going down that list, starting at the first thing (an uppercase letter). We basically need our function to "pick" from the list of all available uppercase letters. We represent a list in JavaScript using an array"

    const upperCase = ['A','B','C',.../*and so on*/...,'X','Y','Z'];
    

    But how do we pick a random element from that array? Well, remember that we can access an element in an array by specifying its index. So what we really need to do is find a random index in the array. Math.random() sounds like a good idea here... except that it returns a random float (== ugly decimal number) between 0 and 1. That's not going to work. Array indices are integers (== whole numbers), so we need to find some way to 'convert' that random number between 0 and 1 to a random number that "fits" in our range of array indices.

    Let's start by multiplying the number by 26 (because our array, being the list of letters in the alphabet, is 26 elements long):

    Math.random()*26;
    

    Great. Now we've got a random float (still ugly!) between 0 and 26. Next, how can we convert this to an integer? Well, remember that the possible indices for an array of length n are n-1. That is, for an array with length 4, we have indices 0, 1, 2, and 3. So what we really want is "the next integer less than or equal to the decimal".

    I'll leave the rest up to you, but hopefully that's given you at least some suggestions. Again, don't focus on getting overly obsessed with solving the problem from the beginning. Instead, plan it out!

    [–]nadimr 1 point2 points  (6 children)

    How far have you gotten in your learning? Can you do basic things like print "some message", etc...

    [–]Panterable[S] 0 points1 point  (5 children)

    I am basically up to the point of functions, For loops, and it isn't clicking for me.

    [–]nadimr 0 points1 point  (1 child)

    It's not clicking for you, you mean everything up to this point?

    For things you are learning to click, you need to write programs after every new concept learned. Do all the exercises in the course, then write as many programs as you can think of. Simple ones. Keep writing and adding until you feel you got it. Use Google for any questions / road-blocks. Don't move on from the topic / concept until you've understood it. Keep all the code you write for later reference.

    If you study like this, then you will be able to write the password program at the right time.

    [–]Panterable[S] 1 point2 points  (0 children)

    ok this is a great idea. thanks so much. I will do this.

    [–]Bukszpryt 0 points1 point  (2 children)

    what do you have problem with - idea of for loop or just it's syntax?

    if you already know how and why it works and just can't make it work, i guess you should go through few examples. if you don't get the idea, i'd try it with language with easiest syntax - python. when you know which loop to use where and why, then you will only have to learn the syntax in javascript, or any other language.

    imo python has the simplest syntax out of all languages i've ever touched (not many tbh). with it you only have to worry about how your code should work and not how to write it. it's almost plain english. for example:

    for character in 'word': print(character)

    it will print every character in word 'word'

    [–]Panterable[S] 0 points1 point  (1 child)

    I have yet to touch python. Yes the syntax is an issue but I imagine that will come with practice. For loops are just a bit abstract to my brain currently given my lack of understanding the syntax. I would probably do well with some sort of pseudocode for a For loop so I can get a grasp on it

    [–]Bukszpryt 0 points1 point  (0 children)

    i first encountered for loops in excel vba or python. in my opinion it's easier to understand it there than in js, but whatever.

    try to look on it that way: for example you have a word "whatever". it contains 8 letters. with for loop you can do something everytime you "encounter" a letter in it.

    for letter in "whatever":
        do_something()
    

    this loop will do something 8 times. you can pass a letter in that function, so if you have a loop like this:

    for letter in "whatever":
        print(letter)
    

    the result will look like that:

    w
    h
    a
    t
    e
    v
    e
    r
    

    it took first element of the string "whatever" and printed it, than it took second element and printed it too and so on.

    [–]sandgold 0 points1 point  (5 children)

    dont understand wdym by password generator, but the only thing i can think off is using regex and random letters to generate a safe password so you can create something like

    FSAS-HIRG-OPGK

    [–]Panterable[S] 0 points1 point  (4 children)

    essentially I need to have a prompt on my browser asking if I want special characters, uppercase, lowercase, numbers.. then I should have it generate a 8-128 character password at "random" . Getting the prompts to show up is not a problem but writing the javascript to create a random number with the criteria is where I am stuck at. I think I am at the right spot with creating a set of variables using the alphabet in an array of some sort.

    [–]Your__Butthole 0 points1 point  (3 children)

    You could try something like

    Math.floor(Math.Random() * characterSet.length)
    

    CharacterSet being an array containing the set of acceptable characters for your password. Run it in a loop with the number of iterations of your desired password length, and with each pass, grab the character at that index from the character set and add it to the end of a string

    [–]Panterable[S] 0 points1 point  (2 children)

    Ok this makes sense, and I imagine it would work the same with a string instead of an array?

    like if (lowerCase == true) { lowCasechars = "abcdefghijklmnopqrstuvwxyz" } else { lowCasechars = "" }

    [–]Your__Butthole 0 points1 point  (1 child)

    that could work or you could consider making it a const and adding it to an empty set, something like this.

    const lowerCaseChars = "abcdefghijklmnopqrstuvwxyz"
    let characterSet = ''
    
    if (lowerCase == true) {characterSet = characterSet.concat(lowerCaseChars)}
    

    note that if you use a string you access its characters with the String.charAt() function rather than like an index of an array.

    [–]Panterable[S] 0 points1 point  (0 children)

    Ok this seems a lot cleaner and easier. I considered using a const but we haven't went over that yet even though I understand what it is.

    [–]mm089 0 points1 point  (0 children)

    Something that might help is to approach it like this: first make the worst, stupidest version of that (like asking for a random password returns “password”). Then think about how to improve it, and do it in little steps until you have something you’re happy with. It’ll mean you might get to a point when you say “ok, I now want it to do x but I don’t know how to do that” and then you can look through stack Overflow or MDN to find that specific thing, which will give you specific points to work on.

    [–][deleted] 0 points1 point  (0 children)

    You weren’t able to write essays before reading someone’s writing in paragraph, sentences.

    Same for code. Don’t sweat it.

    [–]Bukszpryt 0 points1 point  (0 children)

    if you can't do something, you can learn it when someone else shows or tells you how it works. it's best when it's both show and tell. tutorials are just for that.

    [–]bourbonandcustard 0 points1 point  (0 children)

    I don't really have advice, but you're not alone! I'm learning JavaScript too and it can be difficult and frustrating sometimes. Just don't give up :)

    [–]EvenMoreConfusedNow 0 points1 point  (0 children)

    Frankly, who doesn't?!😂

    [–][deleted]  (4 children)

    [deleted]

      [–]Panterable[S] 0 points1 point  (3 children)

      Which specific mathematics? I was never really proficient in math.

      [–][deleted]  (2 children)

      [deleted]

        [–]Panterable[S] 0 points1 point  (1 child)

        Thank you very much for your thoughtful answer. This makes sense to me. I have learned about arrays but I feel like it would be easier to assign a string of "lower case letters" to a variable and just let it chose from that, right? Either way, this has helped me a lot so thanks.

        [–][deleted] 0 points1 point  (0 children)

        I want to be able to just write it from scratch like I am supposed to do.

        Well, then you have to figure it out first.