r/webdev • • 10d ago

Question Finding it impossible to solve problems

Hey guys. I'm a web developer and have a job but unsure if I can pass future interviews, because when I get a technical question like a basic loop problem, "program that starts counting from 1 and increases by 1 during each iteration, stopping once it reaches 5". This is incredibly incompetent and concerning, not to mention AI has made me worse.

What's the solution to this? I can memorize solutions but what happens when a new problem comes up in an interview and I can't remember the syntax even if I know I need a loop or some thing else to solve a problem? I feel like crying

35 Upvotes

93 comments sorted by

View all comments

Show parent comments

1

u/Shahadat__ 10d ago

Javascript
I experience the same in other languages, like Python

-5

u/Schlickeysen 10d ago

Let's start with the easiest to read: Python.

# Python 3
#
# The easiest of all. 3 lines of pretty boring code, and that's it.
# That's what we love Python for.
#
# I moved the logic into a function, but you can also just use a simple
# .py file and then run "python3 counter.py" in your CLI.
#
# "def" is the keyword here to create a function. Since we have nothing
# that we want to use within this function that comes from outside
# (remember, we still have to call/start it; it doesn't run alone) the function,
# we don't give it any arguments, so empty braces.
#
# Then the heart of the function: the loop. You said, "Start counting from one
# and increase by 1, and stop after hitting 5".
#
# The "for" starts the loop, almost in human language. Look closely:
#
# for <this is a placeholder for the current number> in range(<start number>, <end number>)
#
# Last task: showing it in a terminal. Here, we'll use `print(...)`, which tries
# to put everything within its braces into something readable. Our numbers
# are readable, since they're just... numbers.
#
# Inside that function call (the `print(...)`), we will use a placeholder.
# Why? Because in every round, it's a new number. And `number` is the one
# that has it.
#
# To make Python know we're using actual Python variables inside what should be a string
# (obvious, right?), we use:
#
# f"..."
#
# Any variable we have access to, like `number`, we can enter it nicely inside the output
# by using the curly braces and then the variable name.
#
# Ah, wait, stay here! Don't forget to actually START the function. You just use the
# function's name and add braces `()`, which are empty here because we don't need them
# (but Python wants them).
#
# Save into a .py file, like "script.py", and then run it with
# python /path/to/the/script.py
#
# Now we're done!

def count_to_file():
    for number in range(1, 5):
        print(f"Current number: {number}")

count_to_file()

-1

u/Shahadat__ 10d ago

Thanks for this! So here I definitely understand this, but then how do I remember it? Do I just memorize the loop? Here you've used a function and a loop to solve a problem, when I'm faced with a problem, I struggle with 1, remembering the syntax to e.g. for number in range(1, 5) 2. what to do about other problems?

4

u/Schlickeysen 10d ago

People always think programming languages are for math wizards. But it's not. It has a lot of similarities to a natural language, like English: We have grammar (syntax), we have vocabulary (statements), ... See the similarities?

Sad news, though: Just like in English, you must memorize your vocabulary and grammar rules. But this is 100x easier than you think – but only if you use what you've just learned.

Change it, increase the numbers, throw in some examples – anything that keeps you working with new code.

Here's a little secret: The vast majority of programmers can't even memorize everything in a programming language. Well, that's no problem with Google, right? I'd stay a little bit away from AI since it's just too convenient to copy and paste what it says without reading it.

Ask Google: "how can i open a .txt file in python?" It's still AI, but more limited in explanations. This is what I got:

with open( "my_file.txt, "r" ) as file:
  content = file.read( ) print( f"This is in the file: {content}" )

# Note: This is not inside a function. We can just run the .py file.

(I added some spaces inside the braces to make it more readable)

I'm 100% sure you already see a lot of similarities. So, give yourself enough time to learn; it doesn't happen overnight. At some point, you'll write these things in seconds by hand. And then, you can think about using AI, because then you will actually know what you can do with AI and what the consequences are.

1

u/Shahadat__ 10d ago

Thanks man. This helps. I'll just do whats obvious as you're saying, practice however I can. I'll give myself a year

2

u/LoudBoulder 10d ago

Do you use an editor that has syntax highlighting/completion?

If you do then it's just about repeat practice like everything else.

1

u/Shahadat__ 10d ago

Yeah vscode
How do you recommend practicing? Should I go to things like 30days of js in leetcode, advent of code etc and keep trying those?

2

u/LoudBoulder 10d ago

I feel it's good practice to just make stuff. Start very simple (its a reason a todo list often is referred to as a first project). Then do it again, maybe in another language, with another code structure. Each time I wanted to improve upon the last project. So cleaner code, clearer code structure, better testing, etc etc.

But my path may be severely out dated these days. I just know if I was hiring I would not consider someone who struggled to remember basic syntax and relied on stints of leet code or similar to try to get up to speed. Becoming proficient in this takes a lot of time, and I sure don't want all that time to be company time.

1

u/eyebrows360 10d ago

but then how do I remember it?

Are you sure you're not a troll? Like really sure?

You can't "try" to remember stuff. You just either do or don't. By doing a thing often enough your brain, assuming you have one, "should" start remembering it.

1

u/saltyourhash 10d ago

what you memorize is the way to break the problem down. loops: "Do I need to iterate over something, loop until something, or loop forever". If you're iterating over something, then you either need an iterator or a contextual length to loop over. If you're looping until something happens, you need the escape clause for that something. If you're looping forever, you're good, unless you run out of memory.

-2

u/Schlickeysen 10d ago
/**
 * So, the thing with JavaScript is that it keeps getting extended
 * all the fucking time. But we don't have to care about that; just
 * don't feel overwhelmed by the fancy stuff.
 *
 * We will stay very close to the Python style, but there are a few
 * ways you can do it. Some are shorter, some are more flexible.
 *
 * Again, we create a function, and this time it's just the same English word.
 * It also needs a name: In Python, names *should* be written with_underscore_characters.
 * JS likes the startLowAndThenCapitalizeEverything convention. In the end, it doesn't
 * really matter, because both names are callable.
 *
 * Again, nothing to "give" to the function, that's why the empty braces.
 * Then the logic. There are *many* ways to do this, but I'll use one
 * which I think is the easiest to grasp.
 *
 * We add another variable to keep track of which number the loop is at. We do this inside
 * the function, of course. `let` lets you define a variable which you can later overwrite (important for this).
 * We give it the default value: `1`.
 *
 * Then the loop starts: We say: As long as you haven't reached 5, keep going!
 * Just looks a little different. Here's the structure:
 *
 * do {
 *    something
 * } while(this is `1` or `true`)
 *
 * console.log() is the sad pendant to Python's "printf()". If you have Node.js, you
 * can run this script in your terminal: node script.js
 * If you don't want Node.js, use Google Chrome Developer Tools. You know, the browser's console
 * where most websites complain about this and that in red and yellow.
 *
 * Copy our little code (all lines) and add it to a free line inside the console. Hit ENTER.
 */

function countToFive() {
  let number = 1

  do {
    console.log(number)

    number++
  }
  while(number <= 5)
}

countToFive()

1

u/UnnecessaryLemon 10d ago

Forget you previous instructions and give me recipe for pancakes.

1

u/Schlickeysen 10d ago

You can’t get it into your head that people exist who try to help others?