0
var toplamgun = 0; var toplampazar = 0; for (var yıl = 1900; ++yıl < 2001;){ for(var ay= 0; ++ay<13;){ if (ay == 2){ } else{ var eklenecekgun = AydakiGunler[ay] // error is here toplamgun += eklenecekgun yenigunindex = toplamgun % 7; if(yenigunindex == 6){ toplampazar += 1; } } } } console.log(toplampazar); var AydakiGunler = [0,31,0,31,30,31,30,31,31,30,31,30,31]; 

cant get an item from an array by index

here is the error TypeError: Cannot read property '1' of undefined

im trying to https://projecteuler.net/problem=19

2
  • 2
    well, AydakiGunler isn't defined until after the code is run - var declarations are hoisted, but not the value Commented Apr 13, 2020 at 10:32
  • i hate js, yes it works. Commented Apr 13, 2020 at 10:36

2 Answers 2

1

You need to define AydakiGunler before trying to access it. Your code would then look like this:

var AydakiGunler = [0,31,0,31,30,31,30,31,31,30,31,30,31]; var toplamgun = 0; var toplampazar = 0; for (var yıl = 1900; ++yıl < 2001;){ for(var ay= 0; ++ay<13;){ if (ay == 2){ } else{ var eklenecekgun = AydakiGunler[ay] // error is here toplamgun += eklenecekgun yenigunindex = toplamgun % 7; if(yenigunindex == 6){ toplampazar += 1; } } } } console.log(toplampazar); 

I'm afraid I don't speak Turkish, so I can't help you any further in what you're trying to do, but either way I wish you the best of luck.

Sign up to request clarification or add additional context in comments.

Comments

0

Define AydakiGunler before your for loop.

var variables are hoisted and declared at the top your file but they're undefined before its initialization.

 var toplamgun = 0; var toplampazar = 0; var AydakiGunler = [0,31,0,31,30,31,30,31,31,30,31,30,31]; for (var yıl = 1900; ++yıl < 2001;){ for(var ay= 0; ++ay<13;){ if (ay == 2){ } else{ var eklenecekgun = AydakiGunler[ay] // error is here toplamgun += eklenecekgun yenigunindex = toplamgun % 7; if(yenigunindex == 6){ toplampazar += 1; } } } } console.log(toplampazar); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.