Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
basic Javascript While loop and boolean value questions

I would ask my instructor, but whenever I do, he gives me an even more vague answer to my questions, so I'm asking yall for help. We "learned" (i.e. watched videos) about for and while loops and I get it, I feel like I do, but whenever it comes to doing the assignments given, I feel like they don't make sense. Like back in math class in high school, they'd teach you about the problems, but then when it came time to do your homework, the problems were completely different from what you just learned about. For instance, it says the basic while loop structure is:

while(condition is true) {
 //do something
}

But then in this assignment, it gives me:

// Another way to write a while loop is to have a boolean variable 
// where the condition goes and then test every time if you need to
// change the boolean to false.

// Below we have a variable lessThan5 and it is set to true.
// Create a loop that tests if our variable 'j' is less than 5.
// If it is less than 5 then Increment it by 1. If it is not 
// less than 5 then set our lessThan5 variable to be false.
let lessThan5 = true;
let j = 0;

while(lessThan5) {
}

We didn't learn anything about using boolean values in while loops and I feel like I'm meant to infer what to do, and what structure to use and I just have no idea. Aside from the fact I feel like the instructions to many of these questions are poorly worded, which only confuses me more!

So then there's this third one:

// Example of what the number game would look like:
// Couple things to note:
// Math is a built in object in javascript.
// Math.round() will round a decimal number to a whole number.
// Math.random() returns a decimal number between 0 to 1.
// (But not including 1)
function guessNumberGame(guess) {
  let guessing = true;
  let number = Math.round(Math.random() * 100);
  while(guessing) {
    if(guess === number) {
      guessing = false;
    } else {
      guess = Number(prompt("That number didn't work. Try again: "));
    }
  }
}

// Problem 3
// We will give you a number through the 'num' parameter
// Create a while loop that will loop 'num' amount of times.
// For example if num is 3 then your while loop should loop 3 times
// If num is 20 then the loop should loop 20 times.
// Increment k every loop.
let k = 0;
function keepLooping(num) {

}

If this Problem 3 is meant to be related somehow to the number game example, I can't see it. I don't even know what it is I need to be asking. Does this make any sense to anyone? And nobody else is publicly asking questions about any of this, and it's making me feel stupid and like I am the only one too dumb to get what's going on. I was doing really well and ahead of schedule with all this until this point, but just none of this is making any sense to me.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Welcome to programming, JavaScript (JS), and StackOverflow (SO)!

Let's dive into this a little deeper. First, a quick JavaScript primer: in JavaScript, everything can be classified as either an expression or a statement. At a super high and not-technical level:

expression: something that produces a value
statement: an instruction to the computer

(For a much longer explanation, see here)

Often, statements have slots that can take expressions. Loops are a great example of that.

For example, 1 + 1 is an expression, since it produces the value 2. Even more simply, 1 on its own is also an expression, since it produces the value 1. while(/*some expression here*/) is a statement that has a slot for an expression. for(s1, e2, e3) is also a statement that has slots for statements and slots.

So, the while loop acts on an expression, and will continue to loop as long as the value returned by that expression is truthy. truthy and falsey is an interesting concept in JavaScript and can be a whole essay on it's own, but the tl;dr of it is that anything that == true is truthy, and anything that == false is falsey

So for your first question, 0 < 5 == true, while 5 < 5 == false. Thus, if you make the value of j be greater than or equal to 5, the loop will break.

let lessThan5 = true;
let j = 0;

while(lessThan5) {
  // For each cycle of the loop, check if `j` is less than 5
  if (j < 5) {
    // If `j` is less than 5, increment it
    j++; // This is equivalent to saying j = j + 1, or j += 1
  } else {
    // If `j` is not less than 5, set `lessThan5` to `false`
    // Not when the loop goes to iterate again, `false == false`, and it stops
    lessThan5 = false;
  }
}

I think given the above you should be able to solve the third problem. Please let us know if you have trouble with it, show us what you try, and we'll be happy to help some more :)

about 4 years ago · Santiago Gelvez Report

0

Let's take a deep breath and relax. I'm a very senior developer and can't tell -- from your examples -- what's going on here. Maybe that's because your instructor is terrible, maybe it's because you've missed some context in your class, and so it's omitted from the question.

I can answer the two questions you've been given. Hopefully it'll be helpful.

First:

I do not know why your materials claim that a while loop might be written this way. I've completed the assignment, but it seems very odd. But if they want you to complete it, here's a solution.

// Another way to write a while loop is to have a boolean variable 
// where the condition goes and then test every time if you need to
// change the boolean to false.

// Below we have a variable lessThan5 and it is set to true.
// Create a loop that tests if our variable 'j' is less than 5.
// If it is less than 5 then Increment it by 1. If it is not 
// less than 5 then set our lessThan5 variable to be false.
let lessThan5 = true;
let j = 0;

while(lessThan5) {
  if (j >= 5) {
    lessThan5 = false;
  } else {
    j++;
  }
}

Moving on to the second snippet, the second snippet does not, to me, appear to be related to guessNumberGame in any way.

And the solution to "Problem 3" seems useless to me. A loop that doesn't do anything is not useful in real life.

That said, the solution to "Problem 3" is as follows:

// Problem 3
// We will give you a number through the 'num' parameter
// Create a while loop that will loop 'num' amount of times.
// For example if num is 3 then your while loop should loop 3 times
// If num is 20 then the loop should loop 20 times.
// Increment k every loop.
let k = 0;
function keepLooping(num) {
  while(k < num) {
    k++;
  }
}

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!