Arrays-math problem

Hi, guys! Nice to meet you all!

I am having difficulties solving the following math problem:

I would appreciate it if you could help me solve it and give some explanation…

This is as far as I could go but it is not working:

The assignment is to solve it with array methods, for loops, for each, for of…anything that works.

I know it might be too simple for this forum and I’m sorry but I have been struggling with it for too many hours…

Thank you in advance!

If something’s not working then you might want to figure out which exact operation isn’t doing what you want, and then look closer at what you’re doing there.

You could for example insert lots of calls to console.log that explain everything that the program is doing, then you could look at the output of that to help you figure out where/when/how it went wrong.

Make sure to keep a firm plan on what should happen, that’s your reference to what your code needs to do. It seems to me like a somewhat common mistake is to try to write code without having thought about what it should do which obviously leads nowhere.

1 Like

This is exactly what I seem to not be able to do:) That’s why I am on the forum.

What, insert prints? You can do that.

I meant that I tried several ‘plans’ as you said to solve it and nothing worked. I think I am having problem with the logic of the math problem, not that much with the code. I am currently studying what I listed above and it is still hard. That’s why I tried to receive an explanation here :slight_smile:

Stick to one plan.

Make your code do that

Back to the prints though, there are things in that code that don’t add up at all, if you print out what’s being done you’ll find that things aren’t what you mean for them to be, and then you can reconsider what you meant, and where that information exist, adjust it, and continue from there.

You may even find things by reading the code, and considering what was said earlier. But that’s harder, and not really any advantage over simply looking at what happens as it runs.

If you look at someone cook and they put in too many eggs, you can tell. But only if you’re looking. (and know what’s supposed to happen, ie having a plan)

Also, that plan better make sense. You should for example be able to carry it out on paper and get the correct result. If it turns out the plan doesn’t actually add up and you rely on human auto correction of the “that’s what I meant” type, then, there isn’t really a plan after all, implementing a plan like that is going to result in a program that doesn’t add up, and it certainly won’t do the right thing even if you meant it, it’ll do what you wrote.

Regarding making that plan, keep in mind that this is a task that you know how to carry out manually. So you already have a plan by virtue of being a human, you just need to consider what you the human would do, your program should probably not be doing it any differently.

1 Like