1

Сделал я вот такой код:

 const slide = document.getElementById('slider');
    const submit = document.getElementById('submit');
    submit.innerHTML = 'Создать поле' + slide.value + 'x' + slide.value;
    return slide.value;
};
getInputValue();
let h = 0;
const counter = () => h = !h;
const ticTacToe = function() {
    const buttonsArr = document.getElementById('buttonsDiv');
    buttonsArr.innerHTML = '';
    const slider = document.getElementById('slider');
    let innerFlex;
    for(let i = 0 ; i < slider.value * slider.value; i++) {
        if(i % slider.value == 0) {
            innerFlex = document.createElement('div');
            innerFlex.classList.add('flexChild');
            innerFlex.classList.add('flex');
            innerFlex.id = 'innerFlexId' + i;
            innerFlex.style.width = 50 * slider.value + 'px';
            innerFlex.style.height =     50 + 'px';
            buttonsArr.append(innerFlex);
            innerFlex = document.getElementById('innerFlexId' + i);
        }
        const btn = document.createElement('BUTTON');
        btn.class = 'flexChild';
        btn.style.width = 50 + 'px';
        btn.style.height = 50 + 'px';
        btn.onclick = function() {
            this.classList.add (counter() ? 'croses' : 'zeroes');
        };
        innerFlex.append(btn);
    }
    const buttonsArrChildList = buttonsArr.childNodes;
    const testFunc = function() {   
        if(this.line - 1 >= 0  || this.line - 1 >= 0) {
            buttonsArrChildList[this.line - 1].childNodes[this.order - 1].testFunc();
        }
    };
    for(let i = 0;i < buttonsArrChildList.length;i++) { 
        for(let j = 0; j < buttonsArrChildList[i].childNodes.length;j++) {
            const buttonExemple =  buttonsArrChildList[i].childNodes[j];
            buttonExemple.order = j;
            buttonExemple.line = i;
            buttonExemple.onclick = testFunc;
        }
    }
};

Реально ли объявить функцию, в нашем случае testFunc, которая вызывает сама себя? как я понимаю у меня выдает ошибку из-за того, что я вызываю ее в момент когда она еще не объявлена

2
  • 1
    Реально. Вызов функции в самой себе называется рекурсией.
    – letzabelin
    4 дек 2019 в 1:23
  • @1ocke Извините, не могли бы вы тогда подсказать, где у меня ошибка в коде? 4 дек 2019 в 1:37

1 ответ 1

0
const testFunc = function() {    
  if (this.line - 1 >= 0  || this.line - 1 >= 0) {
    testFunc.call(buttonsArrChildList[this.line - 1].childNodes[this.order - 1]);
  }
}

Покажите, как Вы вызываете testFunc в самый первый раз, что такое в ней this, и откуда берутся line и order.

2
  • добавил остальную часть кода 4 дек 2019 в 1:49
  • @LeoMotorBreath Молодец. Мой ответ - правильный.
    – user176262
    4 дек 2019 в 1:59

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.