0

I'm supposed to create a function that will continue to double the first argument until it is greater than or equal to the 2nd argument. Every time it doubles, I add 20 to a counter.

However, for whatever reason, it doesn't seem to be adding 20 to the counter as it always returns 0 and does not print the console.log I included for each loop, which makes me think it's not running the loop.

Why isn't it running the loop and what am I doing wrong?

function bacteriaTime(currentNum, targetNum) {
  let counter = 0
  for (let i = currentNum; i >= targetNum; i *= 2) {
    counter += 20;
    console.log('bacteria count is ' + i + ' and ' + counter + ' have passed.')
  }
  return counter;
  console.log(counter);
}
8
  • 4
    How are you calling it? Anything after return counter will be ignored because... you've returned. Commented Jun 13, 2019 at 21:43
  • 2
    currentNum starts as less than targetNum correct? The condition is backwards then. You want to check for it being less than targetNum. Commented Jun 13, 2019 at 21:43
  • What is currentNum and targetNum? Commented Jun 13, 2019 at 21:43
  • I'd recommend (strongly) that you work through it with a pencil and paper using small numbers as your inputs. "Play computer" and figure out what's actually happening by processing every step. For example, currentNum as 0, targetNum as, say, 3. Commented Jun 13, 2019 at 21:44
  • 1
    @TabbieC developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Nothing particularly vague there. Commented Jun 14, 2019 at 12:06

2 Answers 2

1

You might wanna check if your condition wasn't already met, and therefore, the code has returned. Also your condition is backwards. It should be: for (let i = currentNum; i <= targetNum; i *= 2) {

Sign up to request clarification or add additional context in comments.

Comments

0

Seems like you've mixed up your comparison. In your for loop you had i >= targetNum which with your inputs would almost always be false. Simply switch the operator to <= as below and you should be good. This will mean i is less than targetNum.

function bacteriaTime (currentNum,targetNum){
    let counter = 0
    for (let i = currentNum; i <= targetNum; i *= 2){
        counter += 20;
        console.log ('bacteria count is ' + i + ' and ' + counter+ ' have passed.')
        }
    console.log(counter);
    return counter;
}

Hope that works. It was probably just a simple mix up.

3 Comments

console.log is still after the return.
-_-; yup it was the >= vs <=. Still getting used to that. Thanks.
@TabbieC No problem we have all been there. I will note as Intervalia you will need to move the second console.log to before the return otherwise it won't print. I have updated my response for that. Mark this as the answer so that everyone knows this is solved. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.