Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: JS does not execute the expression for no reason!

1. JS does not execute the expression for no reason!

I have the following function:
Code:
```function sonKare(sayac, oteki) {
var vertical, horizontal = hangiYataySira(sayac, oteki), j, k,
l, m, united, tekRakam, actualV = [], actualH = [],
numaralar = [1, 2, 3, 4, 5, 6, 7, 8, 9], olmayanlar = [];
if(ilkSefer === true) {
vertical = hangiDikeySira(rakamlar.length);
ilkSefer = false;
}
else {
vertical = hangiDikeySira(rakamlar.length - 1);
}
for(j = 0; j <= vertical.length - 1; j ++ ) {
if(rakamlar[vertical[j]] !== undefined)
actualV.push(rakamlar[vertical[j]]);
}
for(k = 0; k <= horizontal.length - 1; k ++ ) {
if(rakamlar[horizontal[k]] !== undefined)
actualH.push(rakamlar[horizontal[k]]);
}
united = actualV.concat(actualH);
//---- Olmayan rakamları bulan kısım ------------
outer: for(l = 0; l <= numaralar.length - 1; l++) {
for(m = 0; m <= united.length - 1; m++) {
if(numaralar[l] === united[m])
continue outer;
if(m === united.length - 1 && numaralar[l] !== united[m])
olmayanlar.push(numaralar[l]);
}
}
//----------------------------------------------
if(olmayanlar.length > 1 || olmayanlar.length === 0) {
rakamlar = [];
document.form1.buton.click(); // THIS LINE DOES NOT WORK SECOND TIME
}
else {
\$("_" + oteki + "_" + sayac).value = olmayanlar[0];
rakamlar.push(olmayanlar[0]);
sayac++;
if(sayac === 10)
return;
else
sonKare(sayac, oteki); //CALLS ITSELF
}
}```
This is a recursive function. As stated above there is a line which does not work for the second time. What I mean is, it works perfect for the first time and after function calls itself, it doesn't work. Javascript does execute the line above it but just disregards that `document.form1.buton.click();` line. I use Firebug for debugging and I checked it many times, when that expression needs to be executed for the second time, JavaScript just ignores it and immediately goes at the end of the function... I just can't figure out why JS does such a thing?
p.s: Don't bother about weird variable names, they are not in English but in Turkish which is my native language

2. Please show us the input element with name "buton" in your HTML code

3. Here it comes:

Code:
`<input type="button" name="buton" class="butonlar" value="Buraya tıkla" onclick="sudoku();" />`

4. Well, since all clicking on that button does is call the sudoku() function, why not simply call the function directly?

I would *GUESS* that the reason it doesn't work is because you are trying to invoke an EVENT on the button object but the current event handler (namely, the existing call to sudoku function) has not yet completed. Surely has something to do with how events are rendered in the DOM. Remember: Invoking an event is not the same thing as simply calling a function. All the event *handlers* that are registered for that event have to be checked and invoked. Even if there is only the one, all the event handling logic still has to be dealt with.

Again, this is pure guess on my part. But I can't see why you need to invoke the event if the real purpose is just to call sudoku() again.

5. The reason why I just didn't call sudoku() function is because sudoku is defined after that sonKare() function. In other words, I was thinking that I would call a function that is not defined. However, it turns out to be irrelevant in my situation because my logic goes like this:
Code:
```sonKare() {
//Something went wrong and sudoku is called
}

sudoku() {
//This is the function which is called at the very beginning of everything
//Something happens and sonKare is called
}```
I thought sonKare() won't recognize the sudoku() function, that's why I use invoke click event. As I am writing I realized that this is also a ridiculous idea Anyway, I now just called sudoku() function and it seems like it works fine... Thank you so much...

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•