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




function monkeyCount(n) 

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

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

 
}

monkeyCount(10)

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;

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.

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