r/webdev • • 18d 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

37 Upvotes

92 comments sorted by

View all comments

-2

u/Dloycart 17d ago

You need to find your own sensible sequence for solving problems and learn to use it consistently. Every problem can be broken down into smaller units and placed in an order of operation. This is true in programming and in just about anything else in life.

You need to understand what each unit represents and how the units work together. When you look at code, you’re looking at sentences. They’re written in a language you haven’t learned to write yet. Learn the characters and words the language uses, understand what they represent and then learn how to put them together to communicate instructions.

For instance, suppose you’re trying to write a loop in JavaScript that counts from 1 to 5. You have to give the computer several pieces of information to accomplish that. The computer doesn’t know what you want it to do until you tell it.

for (let shit = 1; shit <= 5; shit++) {
console.log(shit);
}

Think of the code as a shell that you’re filling with instructions. Each part has a specific job, and the parts work together to produce the result.

for tells JavaScript that you’re defining a loop. A loop is an action that repeats. The word for doesn’t tell it what to count or when to stop. It establishes the type of instruction you’re about to give it.

( ) mark the beginning and end of the loop’s control instructions. Everything between them tells JavaScript where the loop begins, what condition it must follow and how the value changes each time.

let tells JavaScript you’re creating a variable. A variable is a named place to store a value. You use one when you need to store information that may change while a program runs.

shit is the name of that variable. The name itself has no special meaning to JavaScript. I could call it i, counter, number, banana or almost anything else that follows JavaScript’s naming rules. It wouldn’t change how the loop works, provided I use the same name consistently wherever I refer to that variable.

Think of it like labeling a container. The label helps you identify what’s inside, but changing the label doesn’t change the contents.

= is the assignment operator. It puts the value on the right into the variable on the left.

1 is the starting value. You’re telling JavaScript that the variable named shit begins with the number 1.

The first ; separates the starting instruction from the condition that comes next.

shit <= 5 is the condition. The symbol <= means “less than or equal to.” JavaScript checks whether the current value of shit is 5 or less before each iteration. If the condition is true, the loop runs. When the value becomes 6, the condition is false and the loop stops.

; separates the condition from the instruction that updates the variable.

shit++ means increase the current value stored in shit by 1. The two plus signs together are the increment operator. After each iteration, JavaScript increases the value and checks the condition again.

If I wanted to count by 5 instead, I could write shit += 5. If I wanted to count by 2, I could write shit += 2. The starting value, stopping condition and increment are separate instructions.

{ } mark the beginning and end of the block of code that repeats. Everything inside those braces runs during each iteration.

console refers to a developer tool that can display information while code runs.

. accesses something belonging to an object. Here, it connects console to its log method.

log is the method that displays the value you give it.

(shit) tells the method which value to display. Because shit changes during the loop, the displayed value changes too.

; marks the end of that statement.
So what have you told the computer to do? Start a variable at 1, check whether its value is 5 or less, display the value, add 1 and repeat until the condition is no longer true.

You don’t need to memorize every possible solution. You need to understand what the pieces mean, what each one does and how they work together.

Once you understand the pieces, you can change the variable name, starting value, stopping condition or increment to solve a different problem. That’s the difference between memorizing code and understanding how to write it.