Unit 4 Functions Question

Hello. I am new to Javascript. When we are introduced to functions in Unit 4, we learn that they are used primarily to simplify code and avoid repetition, but there are several examples (such as Lesson 10.2, building floors on a building in console.log) where we have to call a function multiple times in a row. Is there no easy way in Javascript to simply call the function and specify how many times we want it to run, instead of having to call it over and over in a row? Maybe just a loop?

The premise is that we write the function once then call it as many times as we need it. If the functionality of the program calls for a loop, then one can be used.

Thanks. I guess I understand what you are saying. If I wanted a function to repeat based upon input, then I would just include that code in the function to take the input and run the function x number of times. It just seems a bit of a clunky way, pedagogically, to present the concept of a function to reduce code, and then have students type the function over several times. The natural reaction would be, “can’t I just make this repeat x times”? Thanks for your response.

You are totally correct that if students had been taught a loop at this point, that would be one solution. So far in the curriculum, however, loops have not been introduced. I agree that if we’re looking at it from a ‘reduce repeated code’ perspective - writing a function multiple times seems counter intuitive, but at least they’re only writing one line three times instead of the 6 lines it contains three times each? I think your natural reaction is a good place to start when motivating the loops lessons in the next unit.

1 Like