מה לא טוב בקוד? (js)

  • הוסף לסימניות
  • #41
אתם צריכים פשוט להשמיט את המאפיין value בבדיקה (שורה 11 בקוד כאן):
JavaScript:
(function(){
    for (let i = 0; i < 8; i++) {
        let row = document.createElement('div')
        row.classList.add('row')
        for (let j = 0; j < 8; j++) {
            let square = document.createElement('div')
            square.classList.add('square')
            row.appendChild(square)
            square.style.backgroundColor = color(i, j)
            if(i != 3 && i != 4)
                if(square.style.backgroundColor == "black") {
                    let hole = document.createElement('div')
                    hole.classList.add('hole')
                    if(i < 3)
                        hole.style.backgroundColor = "black"
                    else hole.style.backgroundColor = "white"
                    square.appendChild(hole)
                }
        }
        document.querySelector('#board').appendChild(row)
  
    }
})()
 
  • הוסף לסימניות
  • #42
אבל השורה הזאת בודקת לפני יצירת העיגול באיזה ריבוע אני נמצאת, כי השחקנים אמורים להיות רק במשבצות השחורות.
אם אני אעשה את השינוי שאמרת ואוריד את השורה הנ"ל יראו את השחקנים השחורים בכל שלושת השורות!!! ואני רוצה לראות אותם רק במשבצות השחורות. (בשחקנים הלבנים לא תהיה בעיה כי אין להם מסגרת ולא יראו אותם במשבצות הלבנות)
 
נערך לאחרונה ב:
  • הוסף לסימניות
  • #43
אבל השורה הזאת בודקת לפני יצירת העיגול באיזה ריבוע אני נמצאת, כי השחקנים אמורים להיות רק במשבצות השחורות.
אם אני אעשה את השינוי שאמרת ואוריד את השורה הנ"ל יראו את השחקנים השחורים בכל שלושת השורות!!! ואני רוצה לראות אותם רק במשבצות השחורות. (בשחקנים הלבנים לא תהיה בעיה כי אין להם מסגרת ולא יראו אותם במשבצות הלבנות)
לא צריך להוריד את כל השורה, רק להוריד את הגישה המיותרת ל- value שמחזירה תוצאה שלא קיימת, הערך נמצא ב-
JavaScript:
square.style.backgroundColor
ולא ב-
JavaScript:
square.style.backgroundColor.value
 
  • הוסף לסימניות
  • #44
אוקי, תודה רבה!!!
 
  • הוסף לסימניות
  • #45
עזרת לי מאוד.
 
נערך לאחרונה ב:
  • הוסף לסימניות
  • #46

אלעזר 1 איך אפשר לעשות לצורה מתוך ה border js ??​

 
  • הוסף לסימניות
  • #47
 
  • הוסף לסימניות
  • #48

@אלעזר 1 תוכל לומר לי למה השימוש בפונקציית slice לא עושה את תפקידה??​

קוד:
if (temp.length > 0) {
                            square.removeChild(hole)
                            let k = 0
                            do {
                                k++
                            } while ((board[k].x != x || board[k].y != y))
                            square.setAttribute('data-exists', 'notExist')
                            board.slice(k, k + 1)
                            console.log(board)
                            toInsertBoard(board, square)
                        }
תודה רבה רבה!!
 
  • הוסף לסימניות
  • #49
האם board הוא מערך?
 
  • הוסף לסימניות
  • #51

@אלעזר 1 עוד שאלה, האם אפשר להסיר id/class שיתווסף (בJS) לתגית שנוצרה בJS???​

 
  • הוסף לסימניות
  • #52
ברור, מערך של אובייקטים.
האם המערך נוצר באופן דינמי?
יתכן ששגיאה במהלך הקוד מונעת את היווצרות המערך, כדאי שתצרפו את הקוד כדי שיהיה אפשר לשחזר את התהליך
 
  • הוסף לסימניות
  • #53

@אלעזר 1 עוד שאלה, האם אפשר להסיר id/class שיתווסף (בJS) לתגית שנוצרה בJS???​

אפשר לקרוא בעזרת JS לאלמנטים שנוספו לדף באמצעות JS,
אך צריך לשים לב שמאזינים לאירועים (events) שהוגדרו בטעינת העמוד באמצעות האזנה ל-class או id לא יכללו את האלמנטים שנוספו אח"כ
 
  • הוסף לסימניות
  • #54
קוד:
let board = [];

(function () {
    for (let i = 0; i < 8; i++) {
        let row = document.createElement('div')
        row.classList.add('row')
        for (let j = 0; j < 8; j++) {
            let square = document.createElement('div')
            square.classList.add('square')
            row.appendChild(square)
            square.style.backgroundColor = color(i, j)
            if (square.style.backgroundColor == "black") {
                square.setAttribute('data-x', j)
                square.setAttribute('data-y', i)
                square.setAttribute('data-exists', 'notExist')
                let hole = document.createElement('button')
                hole.classList.add('hole')
                square.appendChild(hole)
                if (i != 3 && i != 4) {
                    if (i < 3) {
                        square.setAttribute('data-exists', 'black')
                        hole.style.backgroundColor = "black"
                    }
                    else {
                        square.setAttribute('data-exists', 'wheat')
                        hole.style.backgroundColor = "wheat"
                    }
                    hole.addEventListener('click', function examination() {
                        let x = square.getAttribute('data-x') * 1
                        let y = square.getAttribute('data-y') * 1
                        let index = 0, temp = []
                        if (square.getAttribute('data-exists') == 'black') {
                            while (temp.length < 2 && index < board.length) {
                                if ((board[index].x == (x - 1) && board[index].y == (y + 1)) || (board[index].x == (x + 1) && board[index].y == (y + 1)))
                                    if (board[index].exists == 'notExist') {
                                        temp.push(board[index])
                                    }
                                    else if (board[index].exists == 'wheat')
                                        examination();
                                index++
                            }
                        }
                        else if (square.getAttribute('data-exists') == 'wheat') {
                            while (temp.length < 2 && index < board.length) {
                                if ((board[index].x == (x - 1) && board[index].y == (y - 1)) || (board[index].x == (x + 1) && board[index].y == (y - 1)))
                                    if (board[index].exists == 'notExist') {
                                        temp.push(board[index])
                                    }
                                    else if (board[index].exists == 'black')
                                        examination()
                                index++
                            }
                        }
                        if (temp.length > 0) {
                            square.removeChild(hole)
                            let k = 0
                            do {
                                k++
                            } while ((board[k].x != x || board[k].y != y))
                            console.log(k)
                            console.log(board[k])
                            square.setAttribute('data-exists', 'notExist')
                            board.slice(k, k + 1)
                            console.log(board)
                            toInsertBoard(board, square)
                        }

                    })
                }
                else hole.id = 'option'
                toInsertBoard(board, square)
            }
            document.querySelector('#board').appendChild(row)
        }
    }
    console.log(board)
})()
//בחירת הצבע המתאים לכל משבצת
function color(i, j) {
    if (i % 2 == 0)
        if (j % 2 == 0)
            return "white";
        else return "black";
    else if (j % 2 != 0)
        return "white";
    return "black";
}

//מילוי הלוח בכל נתוני המשבצות
function toInsertBoard(board, square) {
    let newSquare = new Square(square.getAttribute('data-x'), square.getAttribute('data-y'), square.getAttribute('data-exists'))
    board.push(newSquare)
}
 
  • הוסף לסימניות
  • #55

@אלעזר 1 אשמח אם תוכל לעזור לי, כתבתי 2 פונק' בjs ויצרתי אפשרות של לחיצה שהיא מפנה לפונק' השנייה, אבל הוא מראה לי את ההפניה והוא נכנס לפונק' אבל כשהוא מגיע ל - onload הוא חוזר לכפתור שנוצר ושם גומר בלי לממש את רב הפונק'. למה???

קוד:
function init() {
    if (localStorage.getItem('gmail') !== null) {
        document.querySelector("#join").innerHTML = "הפניות שלי"
        document.querySelector("#join").href = "allRequest.html"
        let x = document.querySelector("#join")
        x.onclick = function () { allRequest() }
        let new_li = document.createElement('li')
        let new_a = document.createElement('a')
        new_a.innerHTML = 'התנתק'
        new_a.href = 'user.html'
        new_a.onclick = function () { out() }
        new_li.appendChild(new_a)
        document.getElementById('add').appendChild(new_li)
        //document.getElementById('add').appendChild(document.createElement('li').appendChild(document.createElement('a').innerHTML='hhhh'))
    }

function allRequest() {
    let gmail = localStorage.getItem("gmail")
    //יצירת קריאה חדשה
    let req = new XMLHttpRequest();
    //פתיחת הקריאה - סוג וכתובת
    req.open('get', 'https:/localhost:44397/API/contacs/GetUserQuetions/' + gmail, true);
    //שליחת הבקשה
    req.send();
    req.onload = function () {
        console.log(req.responseText);
        let allRequest = JSON.parse(req.response);
        console.log(allRequest);
        let table = document.createElement('table')
        table.setAttribute('class', 'table table-primary table-bordered table-hover')
        //שורת כותרת
        let r = document.createElement('tr');
        //עמודות כותרת
        let d1 = document.createElement('th');
        d1.innerHTML = "שאלה"
        r.appendChild(d1)
        d1 = document.createElement('th');
        d1.innerHTML = "תשובה"
        r.appendChild(d1)
        // הוספת שורת כותרת
        table.appendChild(r)
        //שורות תוכן
        for (let i = 0; i < allRequest.length; i++) {
            r = document.createElement('tr');
            //Question
            d1 = document.createElement('td');
            d1.innerHTML = allRequest[i].Question
            r.appendChild(d1)
            //Answer
            d1 = document.createElement('td');
            if (allRequest[i].Answer != null)
                d1.innerHTML = allRequest[i].Answer
            else d1.innerHTML = "שאלתך טרם נענתה"
            r.appendChild(d1)
            // הוספת שורות תוכן
            table.appendChild(r)
        }
        document.getElementById('contact').appendChild(table)
    }
    // document.getElementById("message").innerHTML = req.responseText
}
}
 
  • הוסף לסימניות
  • #56

@אלעזר 1 אשמח אם תוכל לעזור לי, כתבתי 2 פונק' בjs ויצרתי אפשרות של לחיצה שהיא מפנה לפונק' השנייה, אבל הוא מראה לי את ההפניה והוא נכנס לפונק' אבל כשהוא מגיע ל - onload הוא חוזר לכפתור שנוצר ושם גומר בלי לממש את רב הפונק'. למה???

קוד:
function init() {
    if (localStorage.getItem('gmail') !== null) {
        document.querySelector("#join").innerHTML = "הפניות שלי"
        document.querySelector("#join").href = "allRequest.html"
        let x = document.querySelector("#join")
        x.onclick = function () { allRequest() }
        let new_li = document.createElement('li')
        let new_a = document.createElement('a')
        new_a.innerHTML = 'התנתק'
        new_a.href = 'user.html'
        new_a.onclick = function () { out() }
        new_li.appendChild(new_a)
        document.getElementById('add').appendChild(new_li)
        //document.getElementById('add').appendChild(document.createElement('li').appendChild(document.createElement('a').innerHTML='hhhh'))
    }

function allRequest() {
    let gmail = localStorage.getItem("gmail")
    //יצירת קריאה חדשה
    let req = new XMLHttpRequest();
    //פתיחת הקריאה - סוג וכתובת
    req.open('get', 'https:/localhost:44397/API/contacs/GetUserQuetions/' + gmail, true);
    //שליחת הבקשה
    req.send();
    req.onload = function () {
        console.log(req.responseText);
        let allRequest = JSON.parse(req.response);
        console.log(allRequest);
        let table = document.createElement('table')
        table.setAttribute('class', 'table table-primary table-bordered table-hover')
        //שורת כותרת
        let r = document.createElement('tr');
        //עמודות כותרת
        let d1 = document.createElement('th');
        d1.innerHTML = "שאלה"
        r.appendChild(d1)
        d1 = document.createElement('th');
        d1.innerHTML = "תשובה"
        r.appendChild(d1)
        // הוספת שורת כותרת
        table.appendChild(r)
        //שורות תוכן
        for (let i = 0; i < allRequest.length; i++) {
            r = document.createElement('tr');
            //Question
            d1 = document.createElement('td');
            d1.innerHTML = allRequest[i].Question
            r.appendChild(d1)
            //Answer
            d1 = document.createElement('td');
            if (allRequest[i].Answer != null)
                d1.innerHTML = allRequest[i].Answer
            else d1.innerHTML = "שאלתך טרם נענתה"
            r.appendChild(d1)
            // הוספת שורות תוכן
            table.appendChild(r)
        }
        document.getElementById('contact').appendChild(table)
    }
    // document.getElementById("message").innerHTML = req.responseText
}
}
א. צריך לוודא שכל האלמנטים שמופיעים בקוד נמצאים עם ה-id המתאים בדף
ב. צריך לבדוק בדפדפן את התגובה שחוזרת מהקריאה ולבדוק שהיא מכילה את הערכים שאתם רוצים להכניס לטבלה
 
  • הוסף לסימניות
  • #57
מה יכולה להיות הבעיה אם יצרתי דף אחד של JS וכמה דפים של HTML, לכולם יש onload שמפנה לאותה פונק' בJS, חלק דפים עולים כנדרש וחלק מהדפים (2) נעצרים באמצע הפונק'.
מתוך בדיקה בf12 הוא מראה לי כך:
script.js:70 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')
at init (script.js:70:51)
at a (script.js:63:5)
at onload (messege.html:17:29)
בעוד הסימון של השגיאה מצוין על השורה הזו:
קוד:
 document.querySelector("#join").innerHTML = "הפניות שלי"
ושורה זו רצה חלק בכל הדפים האחרים.
הפונק' הזו לא מקבלת אלמנטים!!
במה הבעיה???
 
  • הוסף לסימניות
  • #58
  • הוסף לסימניות
  • #59
ידוע לך קישור להדרכה בנושא?
תודה
 
  • הוסף לסימניות
  • #60
ידוע לך קישור להדרכה בנושא?
תודה
יש בגוגל הרבה מאד מדריכים ל-JS, ביניהם:
 

פרוגבוט

תוכן שיווקי
פרסומת

אשכולות דומים

רוצים להישאר בעניינים?

אנחנו מזמינים אתכם להצטרף לעשרות אלפי המנויים
שכבר נהנים מתוכן איכותי שמגיע שירות לתיבת המייל שלהם.
ותקבלו את כל הנושאים החמים, שלא תרצו לפספס.

מה תמצאו בניוזלטר של פרוג?


✅ ריכוז תכנים מעניינים בנושא החודש.

✅ טיפים ומאמרים שניתן לקרוא רק בניוזלטר של פרוג!

✅ עדכונים על אירועי קהילה ושיתופי פעולה.

✅ זרקור על פינה בפרוג שטרם הספקתם להכיר.

✅ בקרוב! פינות נוספות בעז"ה.



להרשמה מהירה מלאו פרטים בטופס פה, (ניתן להסיר עצמכם בכל עת):

איזה כיף שהצטרפת אלינו!

Success

מעכשיו, תהיו הראשונים לקבל את כל העדכונים, החדשות, והתכנים הכי חמים שלנו בפרוג!

  • תודה
Reactions: לב נשבר1 //
1 תגובות

הצטרפות לניוזלטר

איזה כיף שהצטרפתם לניוזלטר שלנו!

מעכשיו, תהיו הראשונים לקבל את כל העדכונים, החדשות, ההפתעות בלעדיות, והתכנים הכי חמים שלנו בפרוג!

לוח מודעות

הפרק היומי

הפרק היומי! כל ערב פרק תהילים חדש. הצטרפו אלינו לקריאת תהילים משותפת!


תהילים פרק כה

אלְדָוִד אֵלֶיךָ יי נַפְשִׁי אֶשָּׂא:באֱלֹהַי בְּךָ בָטַחְתִּי אַל אֵבוֹשָׁה אַל יַעַלְצוּ אֹיְבַי לִי:גגַּם כָּל קוֶֹיךָ לֹא יֵבֹשׁוּ יֵבֹשׁוּ הַבּוֹגְדִים רֵיקָם:דדְּרָכֶיךָ יי הוֹדִיעֵנִי אֹרְחוֹתֶיךָ לַמְּדֵנִי:ההַדְרִיכֵנִי בַאֲמִתֶּךָ וְלַמְּדֵנִי כִּי אַתָּה אֱלֹהֵי יִשְׁעִי אוֹתְךָ קִוִּיתִי כָּל הַיּוֹם:וזְכֹר רַחֲמֶיךָ יי וַחֲסָדֶיךָ כִּי מֵעוֹלָם הֵמָּה:זחַטֹּאות נְעוּרַי וּפְשָׁעַי אַל תִּזְכֹּר כְּחַסְדְּךָ זְכָר לִי אַתָּה לְמַעַן טוּבְךָ יי:חטוֹב וְיָשָׁר יי עַל כֵּן יוֹרֶה חַטָּאִים בַּדָּרֶךְ:טיַדְרֵךְ עֲנָוִים בַּמִּשְׁפָּט וִילַמֵּד עֲנָוִים דַּרְכּוֹ:יכָּל אָרְחוֹת יי חֶסֶד וֶאֱמֶת לְנֹצְרֵי בְרִיתוֹ וְעֵדֹתָיו:יאלְמַעַן שִׁמְךָ יי וְסָלַחְתָּ לַעֲוֹנִי כִּי רַב הוּא:יבמִי זֶה הָאִישׁ יְרֵא יי יוֹרֶנּוּ בְּדֶרֶךְ יִבְחָר:יגנַפְשׁוֹ בְּטוֹב תָּלִין וְזַרְעוֹ יִירַשׁ אָרֶץ:ידסוֹד יי לִירֵאָיו וּבְרִיתוֹ לְהוֹדִיעָם:טועֵינַי תָּמִיד אֶל יי כִּי הוּא יוֹצִיא מֵרֶשֶׁת רַגְלָי:טזפְּנֵה אֵלַי וְחָנֵּנִי כִּי יָחִיד וְעָנִי אָנִי:יזצָרוֹת לְבָבִי הִרְחִיבוּ מִמְּצוּקוֹתַי הוֹצִיאֵנִי:יחרְאֵה עָנְיִי וַעֲמָלִי וְשָׂא לְכָל חַטֹּאותָי:יטרְאֵה אוֹיְבַי כִּי רָבּוּ וְשִׂנְאַת חָמָס שְׂנֵאוּנִי:כשָׁמְרָה נַפְשִׁי וְהַצִּילֵנִי אַל אֵבוֹשׁ כִּי חָסִיתִי בָךְ:כאתֹּם וָיֹשֶׁר יִצְּרוּנִי כִּי קִוִּיתִיךָ:כבפְּדֵה אֱלֹהִים אֶת יִשְׂרָאֵל מִכֹּל צָרוֹתָיו:
נקרא  2  פעמים
למעלה