Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript JS - For Loop Question

Hi there!

I have a newbie question. My task is the following.

There is the array "courses" consisting of a unknown number of arrays with strings that show the students of the course. I want to find out which course in "courses" is the smallest one. My thinking was the following.

JavaScript:
// Find out how many courses currently exist
        var coursesCount = courses.length
      
        // Compare the number of students of each course with the next one and filter out the smallest one
        for (var i = 0; i <= coursesCount; i++){
            console.log(courses[I].length)
            if (i == 0){
                var smallestCourse = courses[I];
            }
            else {
                if (courses[I].length < smallestCourse.length){
                    smallestCourse = course[I];  //red line (does not show in code block)
                }
            }
        }

// Tell me what members are in the smallest course
console.log(smallestCourse)

The problem is that I always receive the error message: Uncaught ReferenceError: course is not defined at red line
I cannot wrap my head around it. Why should course be undefined since the loop is running all numbers from 0 til the max number of courses?
 
Last edited by a moderator:
Heya,

So from a quicker overview, it just looks like you've given a little bit of a typo; rather than spelling courses, you've written it as course (missing the extra 's').

With that mentioned, there are a couple of other things I'd like to mention/point out in your code.

1. I'm a little bit worried about your course, and it appears to recommend the usage of 'var', the usage of 'var' is now outdated and actually recommended to be avoided in most situations. You should instead be defining your variables using one of the below (you can mix and match)

1. let variableName - Used if you plan on changing this variable's data.​
2. const variableName - Used if you plan on keeping the variables data the same throughout the lifespan of the program.​
2. When making use of conditional statements (if blocks) in JS it's recommended to rather make use of === as apposed to ==; the reason this is recommended is that when you make use of the double equal sign we open the risk of bugs as it does not care about data types, for example if(0 == "0") //This is true but logically we should expect this to be false because one is an int and the other is a string, however, when you use the triple equal sign JS checks for both value and data type.

I hope this helped! Let me know if the above solution fixed the problem or if you have any additional questions!
 
Back
Top Bottom