For loop (goodStrawberries) variations

#1

Both of these work and I can’t explain to myself why exactly :S

[A]
var goodStrawberries = 500;
for (var i = 1, j = goodStrawberries; i <= j; i++) {
if(i % 5 === 0){
goodStrawberries -= 1; }}
console.log(goodStrawberries);

[B]
var goodStrawberries = 500;
for(i = 1; i < goodStrawberries;i++){
if(i%4===0){
goodStrawberries-=1;}}
console.log(goodStrawberries);

#2

What do you mean by work? Is there some reason why they wouldn’t do that? Is there some particular part there that you’d like to ask about so that you can read them yourself?
And, if you want others to read your code, make sure to give them an exact copy so that they’re not reading something else.

#3

I revised the code presentation above.

They both produce the same result of reducing goodStrawberries variable by 1 at every 5th loop iteration.

I don’t understand how [B] works using i%4, when [A] works using i%5.

In the lesson example Good Strawberries, both answers were accepted.

#4

No they don’t. And you can show that by printing out whether it does that or not each iteration. (And try to find where you went wrong when you came up with that conclusion!)

That’s not the only thing you changed between those two.

Both pieces of code are very cryptic, I’m not sure what the exercise is asking of you, but there must be a far better way to express it than this where, for starters, it’s clear how many iterations are being made.

#5

I see what you mean now, I did not provide any real context. The exercise (which I can’t now access for some reason) stated every 5th strawberry was bad, and had to be deducted from goodStrawberries to give a final number of good strawberries.

I came up with answer [A], but I saw answer [B] proposed on a forum and I went back to the lesson, and yes, it was an accepted answer… yet it does not go through up to 500. Maybe because it gives a final count of 400 (500/5=100; 500-100=400), which I presume the lesson algorithm is looking for?!

Perhaps I was on the right track and should have moved on, but I was interested to see other’s solutions, and so now I’m confused as to wether [B] is a legitimate way to address the problem or not, and if not, I need to be aware the lesson environment is a bit more limited in answer-checking than I thought.

Thank you for your responses, they prompted me to discover a way to log each loop iteration to the console, which I didn’t know how to do before

[A]
var goodStrawberries = 500;
var p = “”;

for (var i = 1, j = goodStrawberries; i <= j; i++) {
if(i % 5 === 0){
p += i + " ";
goodStrawberries -= 1; }}

console.log§;
console.log(goodStrawberries);

gives:

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360 365 370 375 380 385 390 395 400 405 410 415 420 425 430 435 440 445 450 455 460 465 470 475 480 485 490 495 500
400

[B]
var goodStrawberries = 500;
var p = “”;

for(i = 1; i < goodStrawberries;i++){
if(i%4===0){
p += i + " ";
goodStrawberries-=1;}}

console.log§;
console.log(goodStrawberries);

gives:

4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 180 184 188 192 196 200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268 272 276 280 284 288 292 296 300 304 308 312 316 320 324 328 332 336 340 344 348 352 356 360 364 368 372 376 380 384 388 392 396 400
400

#6

You should always be able to argue for why something is correct, if not, it’s probably not, and it’s questionable how you came up with it at all. If you look at what it prints, it’s every 4th, and while it still comes up with a result of 400, doesn’t that seem more like coincidence than something you thought out? And, would it hold if you changed 500 to some other number? If you were removing every 5th, you’d say “yes of course” but if you’re getting the same result by coincidence…

If you don’t specifically need to know the numbers, then removing 20% would be quicker (and paying attention to which way that should be rounded)

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.