ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingมีปัญหาโปรแกรม Javascript ทำงานหลุดกลางคั่น ใครเก่ง javascript ช่วยทีครับ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: มีปัญหาโปรแกรม Javascript ทำงานหลุดกลางคั่น ใครเก่ง javascript ช่วยทีครับ  (อ่าน 1196 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
program53
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 2



ดูรายละเอียด
« เมื่อ: 16 มิถุนายน 2011, 15:02:55 »

มีปัญหาโปรแกรม Javascript ทำงานหลุดออกจากโปรแกรม ทั้งที่โปรแกรมยังไม่จบ ผมได้เอาโปรแกรมที่เขียนมาให้ดูด้วยครับ
ผมเขียนเป็น Javascript Function ดัง Code ด้านล่าง
//=========================================================
function RandNo(argTableSize,argRandNoFlag) {

var vfArrayCell= new Array(Array());
var vfArrayNoCount= new Array();
for (row=0;row<argTableSize;row++) {
      for (col=0;col<argTableSize;col++) {
          if (argRandNoFlag === 0) {
               vfArrayCell[row][col] = "&nbsp";  //strval(rand(0,9));
         }
         else {
          //==> Check Rand No. Count <= 3
            do {
                   vfRandNo = Math.round(Math.random()*9);
                  if (typeof vfArrayNoCount[vfRandNo] =='undefined') {
                  if (row==1) {alert("a");}  //==> หลุดจุดที่ 1 โปรแกรมจะหยุดทำงาน
                     vfArrayNoCount[vfRandNo]=1;
                  vfArrayCell[row][col]=vfRandNo;
                     alert("Arow="+row+" col="+col+" randno="+vfRandNo+" vfArrayNoCount["+vfRandNo+"]="+vfArrayNoCount[vfRandNo]+
                       " Cell"+row+col+"="+vfArrayCell[row][col]);
                  break;
               }
               else {
                  if (row==1) {alert("b");} //==> หลุดจุดที่ 2 โปรแกรมจะหยุดทำงา
                       if (vfArrayNoCount[vfRandNo]+1<=3) {
                        vfArrayNoCount[vfRandNo]=vfArrayNoCount[vfRandNo]+1;
                     vfArrayCell[row][col]=vfRandNo;
                  alert("Brow="+row+" col="+col+" randno="+vfRandNo+" vfArrayNoCount["+vfRandNo+"]="+vfArrayNoCount[vfRandNo]+
                       " Cell"+row+col+"="+vfArrayCell[row][col]);
                          break;
                  }
                 }            
               }
            while (true);
          //====
         }
    }
}

//for (row=0;row<argTableSize;row++ ) {
//      for (col=0;col<argTableSize;col++ ) {
//      vfRandNo=vfRandNo+vfArrayCell[row][col];
//      }
//}

alert("eeeeee");
return vfRandNo;
}
//==========================================

เมื่อเรียก Function นี้ทำงาน โปรแกรมจะหลุดเมื่อขึ้น row=1  ตามที่ผมทำ Comment ไว้ในบรรทัดที่หลุดออก ดูบรรทัดที่ผมทำ Comment หลังคำสั่งดังนี้นะครับ
//==> หลุดจุดที่ 1 โปรแกรมจะหยุดทำงาน
//==> หลุดจุดที่ 2 โปรแกรมจะหยุดทำงาน
ใครตอบที่ว่า ทำไมก่อนออกจาก Function ไม่พิมพ์ alert("eeeee");
ช่วยหน่อยนะครับ งงจริงๆ


บันทึกการเข้า
bonshington
ก๊วนเสียว
*

พลังน้ำใจ: 47
ออฟไลน์ ออฟไลน์

กระทู้: 376



ดูรายละเอียด
« ตอบ #1 เมื่อ: 16 มิถุนายน 2011, 15:33:19 »

มันผิดตั้งแต่บรรทัดแรกละคับ

var vfArrayCell = new Array(Array());
คือไรอ่ะ? ไม่เคยใช้ ถ้าจะประกาส array  ซ้อน array แบบนี้ดีกว่าคับ
var vfArrayCell = [];

จากนั้น เวลาประกาศแต่ละ element ให้
vfArrayCell.push([]);
ของ js array, stack, queue มันจะอันเดียวกัน

if (typeof vfArrayNoCount[vfRandNo] =='undefined') {
มี type undefine ด้วยหรอ? ถ้าจะดูว่า object เป็น null ให้
if(!vfArrayNoCount[vfRandNo]) ต้องไม่เป็น null หรือใส่ != null

if (row==1) {alert("a");}  //==> หลุดจุดที่ 1 โปรแกรมจะหยุดทำงาน
คือ ที่ for loop ลองใส่ var row เพราะเหมือนว่า row ยังไม่ได้ประกาศใช้

 vfRandNo = Math.round(Math.random()*9);
ลืมใส่ var หน้า vfRandNo

vfRandNo=vfRandNo+vfArrayCell[row][col];
ด้านบน ใส่ vfArrayCell เป็น "&nbsp;" แต่ vfRandNo เป็นตัวเลข สรุป จะเอาผลลัพธ์เป็นตัวเลข หรือ string?
อ่าน code ไปได้นิดนึง เลิกอ่านคับ เพราะแต่ละบรรทัด ไม่มี comment และ แต่ละคำสั่ง meaning less เหลือเกิน คือ อ่าน codeแล้ว ไม่เข้าใจว่าต้องการทำอะไร

ติอีกนิด
- เอา alert ใส่ใน loop ไม่workคับ
- do while แล้วใช้ break เป็นตัวออก ไม่work อย่างแรง
-

อ่านcodeคร่าวๆ แล้วงงๆ เหมือนว่าจะพยายามrandomเลขใส่ลงเมตริก แล้วrandom เอาออกมา หรือว่ายังไงคับ?
« แก้ไขครั้งสุดท้าย: 16 มิถุนายน 2011, 18:51:16 โดย bonshington » บันทึกการเข้า
program53
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 2



ดูรายละเอียด
« ตอบ #2 เมื่อ: 16 มิถุนายน 2011, 15:51:44 »

ขอบคุณ bonshington ที่ช่วยตอบปัญหาและ comment มา Code ที่ผมแสดงให้ดู
มันทำงานได้ในรอบแรกที่ row=0 แต่พอ row=1 มันจะหลุดจากโปรแกรม
จากที่คุณ comment มา ผมแก้เรื่อง var vfRandNo แล้วตามที่บอกมา และส่วน row
ผมเอามาจากที่ประกาศ row ในคำสั่ง for ผมว่าตัวแปร row ไม่น่าเกี่ยวอ่ะครับ
ตอนนี้แก้บางอย่างแล้ว ก็ยังหลุดเหมือนเดิม

บันทึกการเข้า
npbook
Newbie
*

พลังน้ำใจ: 3
ออฟไลน์ ออฟไลน์

กระทู้: 51



ดูรายละเอียด เว็บไซต์
« ตอบ #3 เมื่อ: 16 มิถุนายน 2011, 15:55:02 »

คุณใช้ตัวแปร vfArrayCell[row] ที่ยังไม่ถูกกำหนดค่าครับ ทำให้เกิด error แล้วเด้งหลุดจากฟังค์ชั่น
ต้องกำหนดค่าให้มันก่อน
โค๊ด:
for (row=0;row<argTableSize;row++) 
    {
        vfArrayCell[row] = new Array(); // เพิ่มบรรทัดนี้เข้าไป
        for (col=0;col<argTableSize;col++)
        {
            if (argRandNoFlag === 0)
            {
........
......
...


แต่หลังจากนั้นจะเกิด infinity loop ครับ ลองเช็ค algorithm ดูให้ดี
บันทึกการเข้า

NPBook
ossytong
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 138
ออฟไลน์ ออฟไลน์

กระทู้: 1,151



ดูรายละเอียด
« ตอบ #4 เมื่อ: 16 มิถุนายน 2011, 15:55:50 »

ที่ไม่ alert("eeeee");

เพราะที่มันจบการทำงานไม่ใช่เพราะ โปรแกรมจบ แต่เกิด error ใน บรรทัดก่อนหน้า alert("eeeee");

ให้ค่อยๆ ย้อนกลับไปทีละนิสๆๆๆ เอาส่วนการทำงานออกก่อน แล้ว ใส่ alert debug โปรแกรมเข้าไปว่า การทำงานของ loop มีปัญหาหรือไม่

ถ้าไม่มีก็ค่อยใส่ code ส่วนทำงาน ทีละบรรทัด แล้ว alert ออกมาว่า ผลได้ตามที่ต้องการหรือไ่ม่ ทำไปเรื่อยๆไล่ไปทีละช้าๆ

แบ่งแยกแล้วเอาชะน  wanwan019 wanwan019
บันทึกการเข้า
solutioncom
Verified Seller
สมุนแก๊งเสียว
*

พลังน้ำใจ: 35
ออฟไลน์ ออฟไลน์

กระทู้: 543



ดูรายละเอียด
« ตอบ #5 เมื่อ: 16 มิถุนายน 2011, 16:30:48 »

สู้ๆครับ wanwan003
บันทึกการเข้า
bonshington
ก๊วนเสียว
*

พลังน้ำใจ: 47
ออฟไลน์ ออฟไลน์

กระทู้: 376



ดูรายละเอียด
« ตอบ #6 เมื่อ: 16 มิถุนายน 2011, 17:07:30 »

เพิ่ม if (!vfArrayCell[row])vfArrayCell[row] = []; ตรงจุดที่หยุดทำงาน
เพราะคุณสร้าง array แต่ไม่ได้ init แต่ละ element

โค๊ด:
function RandNo(argTableSize, argRandNoFlag) {

var vfArrayCell = new Array(Array());

var vfArrayNoCount = new Array();
for (var row = 0; row < argTableSize; row++) {
for (var col = 0; col < argTableSize; col++) {
if (argRandNoFlag === 0) {
vfArrayCell[row][col] = "&nbsp";  //strval(rand(0,9));
}
else {
//==> Check Rand No. Count <= 3
do {
var vfRandNo = Math.round(Math.random() * 9);

if (typeof vfArrayNoCount[vfRandNo] == 'undefined') {
//    if (row == 1) {
//        alert("a");
//                        }  //==> หลุดจุดที่ 1 โปรแกรมจะหยุดทำงาน

   vfArrayNoCount[vfRandNo] = 1;

   if (!vfArrayCell[row])
       vfArrayCell[row] = [];

vfArrayCell[row][col] = vfRandNo;

document.body.innerHTML
                        += "\nArow=" + row +
" col=" + col +
" randno=" + vfRandNo +
" vfArrayNoCount[" + vfRandNo + "]=" + vfArrayNoCount[vfRandNo] +
" Cell" + row + col + "=" + vfArrayCell[row][col];

break;
}
else {
   
if (vfArrayNoCount[vfRandNo] + 1 <= 3) {

   vfArrayNoCount[vfRandNo] = vfArrayNoCount[vfRandNo] + 1;

   if (!vfArrayCell[row])
       vfArrayCell[row] = [];

vfArrayCell[row][col] = vfRandNo;
document.body.innerHTML
+= "\nBrow=" + row +
" col=" + col +
" randno=" + vfRandNo +
" vfArrayNoCount[" + vfRandNo + "]=" + vfArrayNoCount[vfRandNo] +
" Cell" + row + col + "=" + vfArrayCell[row][col];
break;
}
}
}
while (true);
}
}
}

//for (row=0;row<argTableSize;row++ ) {
//      for (col=0;col<argTableSize;col++ ) {
//      vfRandNo=vfRandNo+vfArrayCell[row][col];
//      }
//}

alert("eeeeee");
return vfRandNo;
}

Arow=0 col=0 randno=2 vfArrayNoCount[2]=1 Cell00=2 Arow=0 col=1 randno=8 vfArrayNoCount[8]=1 Cell01=8 Brow=0 col=2 randno=8 vfArrayNoCount[8]=2 Cell02=8 Brow=0 col=3 randno=2 vfArrayNoCount[2]=2 Cell03=2 Arow=0 col=4 randno=7 vfArrayNoCount[7]=1 Cell04=7 Brow=1 col=0 randno=7 vfArrayNoCount[7]=2 Cell10=7 Brow=1 col=1 randno=8 vfArrayNoCount[8]=3 Cell11=8 Arow=1 col=2 randno=0 vfArrayNoCount[0]=1 Cell12=0 Brow=1 col=3 randno=0 vfArrayNoCount[0]=2 Cell13=0 Arow=1 col=4 randno=5 vfArrayNoCount[5]=1 Cell14=5 Brow=2 col=0 randno=0 vfArrayNoCount[0]=3 Cell20=0 Brow=2 col=1 randno=7 vfArrayNoCount[7]=3 Cell21=7 Brow=2 col=2 randno=5 vfArrayNoCount[5]=2 Cell22=5 Arow=2 col=3 randno=1 vfArrayNoCount[1]=1 Cell23=1 Arow=2 col=4 randno=9 vfArrayNoCount[9]=1 Cell24=9 Brow=3 col=0 randno=2 vfArrayNoCount[2]=3 Cell30=2 Arow=3 col=1 randno=6 vfArrayNoCount[6]=1 Cell31=6 Arow=3 col=2 randno=4 vfArrayNoCount[4]=1 Cell32=4 Brow=3 col=3 randno=6 vfArrayNoCount[6]=2 Cell33=6 Arow=3 col=4 randno=3 vfArrayNoCount[3]=1 Cell34=3 Brow=4 col=0 randno=6 vfArrayNoCount[6]=3 Cell40=6 Brow=4 col=1 randno=1 vfArrayNoCount[1]=2 Cell41=1 Brow=4 col=2 randno=9 vfArrayNoCount[9]=2 Cell42=9 Brow=4 col=3 randno=3 vfArrayNoCount[3]=2 Cell43=3 Brow=4 col=4 randno=3 vfArrayNoCount[3]=3 Cell44=3

ออกละคับ
« แก้ไขครั้งสุดท้าย: 16 มิถุนายน 2011, 17:10:14 โดย bonshington » บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์