Var i = 0 in for loops is giving me undefined?

The problem Its asking me to solve is this

Here is a picture of one solution I tried but for some reason it wont let me start my loop from 1

i.e for(i=1;i<=n;i++) returns undefined for some reason ?

image

tried a different method to solve the problem but Im getting something wrong in this too when I submit it to the ide

image

image

UUUUGH WHY

Ill link the problem so you can see what Im looking at properly

V HERE V

It would be much better if you could actually post the text code so we can test it out, instead of providing a screenshot.

When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like



function monkeyCount(n) 

{
  var _monkeyArray = []
  for ( i = 1 ; i <= n; i++)
  {
    _monkeyArray.push(i);

    if(_monkeyArray.length>n)
    {
     
     return _monkeyArray;
    }
 
    
  
  }

 
}

monkeyCount(10)

The reason the above code does not work, is that your if statement condition testing if _monkeyArra.length < n will never be true, so the return _monkeyArray line never executes. When a JS function does not return something explicitly, then undefined is returned by default.

If your for loop is only adding n items, then why would you think the length of _monkeyArray would ever be greater than n (the number of items in the array)?

EDIT: If you comment out the following lines of code:

    if(_monkeyArray.length>n)
    {
     
     return _monkeyArray;
    }

then _monkeyArray will contain the correct numbers in it. Then, you simply need to return _monkeyArray at the end of the function.

2 Likes

Why doesn’t this version work?


function monkeyCount(n) 

{
  var _monkeyArray = []
  for ( i = 1 ; i <= n; i++)
  {
    _monkeyArray.push(i);

    

    if(_monkeyArray.length=n)
    {
     console.log (_monkeyArray);
    }

      
 
  
  }

 
}

You did not not declare a variable. i = 1; should be var i = 1;

You are assigning n to _monkeyArray.length instead of comparing if _monkeyArray.length and n are equal.

Also, like I stated in the last reply, _monkeyArray will have all the correct numbers in it once the for loop finishes, so you do not need to make the check you are attempting. Instead, simply return _monkeyArray before the end of the function.

This does not change the outcome of the code written.

The if statement is not necessary. remove the if statement and add a return _monkeyArray out the for loop.

function monkeyCount(n){
  var _monkeyArray = [];
  for (var i = 1 ; i <= n; i++){
    _monkeyArray.push(i);
  }
  return _monkeyArray; 
}

Yh. didn’t notice the if statement.

Read my response again. I told you where to put the return statement.

1 Like

In the screenshot you shared of your code (which you have now deleted), you were not returning anything inside your function, so JS by default returns undefined.

1 Like

Please format your braces like:

function monkeyCount(n){

Instead of:

function monkeyCount(n)
{

It just makes it easier to read and more formal.

Its also less of a eye sore.

1 Like

That is a subjective choice. While I agree that I prefer the curly brace on the same line as the function, that is simply a convention. In other languages (like C#, and the .net family, afaik) the opposite is the convention. I think ESLint defaults to same-line, but that is not holy writ. There are other styles that require separate line braces.

If you become a dev, you will see different styles. I recently was coding with a team that was coding in a very different style than that with which I was familiar. Rather than argue with the 5 of them and change them to my way of thinking, I just did it their way - it’s not that hard. Similarly, I recently did a coding challenge for an interview. The code they gave me was using different naming conventions than I’m used to. Did I argue with them, convincing them that I’m a pain in the ass that won’t compromise? No, I just did it their way.

Well its not like we are doing anything that big, its a simple placement of the curly bracket. I would be able deal with it, but suggest to them doing it the way i have seen everywhere but here.

Its one curly bracket, does it really need another line. This is not talking about the ending one, because you need to see where a function ends clearly. You know a function starts with the function keyword.

Again, it’s a stylistic choice. If you’re used to it a certain way, the other way seems weird. And vice versa. Neither way is right or wrong. True, in JS, the same line is probably more common but that is not to say that it must be that way. I have seen JS coders that do it on the next line.

There are arguments for the nextlinerers - it breaks up the code more and prevents the code block from visually mingling with the function definition/parameters.

If you want there are plenty of threads on other sites debating issues like this and tabs vrs spaces and how many spaces, etc. with an angry ferocity that make the Shia and Sunni conflict look like a Girl Scout picnic.

But it is a subjective choice, not an immutable law of coding. You prefer it one way. Fine. Accept that others may disagree. We accept everyone here, even people that prefer spaces over tabs, the demon spawn they may be.

Learn to adapt and accept - it’s going to make your coding life a lot easier,

Yeah it isn’t a big deal, and i’m fine with it. Its like walking down a empty sidewalk on the left instead of right. Doesn’t make a difference, and it all depends on wheres you are from.

image

I dunno I just prefer it because it lets me see the order and sections of the code more easily at a glance