2017-10-07 8 views
0

私はちょうどそれを2つのクラスを持っていた、javascriptを学ぶようになった。だから私の知識は非常に限られています。しかし、私は予定のアプリケーションを作成しようとしていると私はUncaught rangeErrorを受け取り続ける:無効な文字列の長さのエラー、と私はそれを修正する理由や考え方がありません。基本的に私はそれに多くの説明なしでコピーするいくつかのコードを与えられているので、誰もがこのエラーで私を助けることができれば大いに感謝します。エラーが現れているコードは以下の通りです。私はそれがラインテーブル+ + appt.tableRow()ですと信じています。これが問題の原因となっています。問題はshowTable関数であるとして、それが与えられる必要がある場合は、明らかに、このコードによりますが、確かではないがあるUncaught rangeError:無効な文字列の長さ

編集:私はちょうどそれより簡単

var Appointment = function(subject, description,date, time) { 
this.subject = subject; 
this.description = description; 
this.datetime = new Date(date + " " + time); 
this.completed = false; 

}; 

Appointment.prototype.isDue = function(){ 
var now = new Date(); 
if(this.datetime > now){ 
    return false; 
} else { 
    return true; 
} 
}; 

Appointment.prototype.whenDue = function(){ 
return this.datetime - new Date(); 
} 

Appointment.prototype.toString = function(){ 
var s = this.subject +'\n'+ 
    this.datetime.toString() + '\n'; 
if(this.completed){ 
    s +="Not Completed\n\n"; 
} 
return s 
}; 

Appointment.prototype.howManyDaysTill = function() { 
var ms = (this.datetime - new Date())/24/60/60/1000 
return ms; 
}; 

Appointment.prototype.howManyHoursTill = function() { 
var hours = (this.datetime - new Date()) /60/60/1000 
return hours; 
}; 

Appointment.prototype.getDate = function() { 
return this.datetime.toDateString(); 
}; 

Appointment.prototype.getTime = function(){ 
return (this.datetime.getHours()) + ":" + (this.datetime.getMinutes()); 
}; 

Appointment.prototype.tableRow = function(){ 
var tr = "<tr><td>" + this.getDate() + "</td><td>" + 
    this.getTime() + "</td><td>" + this.subject + 
    "</td></tr>"; 
return tr; 
}; 

var appointments = []; 

window.onload = function(){ 
var newButton = document.getElementById("new"); 
newButton.onclick = function() { 
    var subj = prompt("Enter a subject title for the appointment"); 
    var desc = prompt("Enter a description for the appointment"); 
    var date = prompt("Enter the appointment date in the format (e.g) 'Sep 
25, 2012"); 
    var time = prompt("Enter the appointment time in the format hh:mm"); 
    var a = new Appointment((subj,desc,date,time)); 
    appointments.push(a); 
    return showTable(); 
}; 

var showTable = function() { 
var tableDiv = document.getElementById("table"), 
    table = "<table border='1'>" + 
     "<thead><th>Date</th><th>Time</th><th>Subject</th><th>Completed</th> 
</thead>"; 
for (var i = 0, j = appointments.length; i < j; j++) { 
    var appt = appointments[i]; 
    table += appt.tableRow(); 
} 
table += "</table>"; 
tableDiv.innerHTML = table; 
}; 


} 

を作るために全体のJavaScriptコードを追加しましたHTML5

<!DOCTYPE html> 
<html lang="en" xmlns="http://www.w3.org/1999/html"> 
<head> 
<meta charset="UTF-8"> 
<script type="text/javascript" src="appointments.js"></script> 
<title>Appointments</title> 
</head> 
<body> 
<button id="new">New Appointment</button> 
<div id ="table"></div> 
<header> 
    <h1> 
     Appointments Book 
    </h1> 
    <p> Enter appointment details and press OK to add, Cancel to revert.</p> 
</header> 
<table> 
<tr> 
    <td>Subject : </td> <td>input type="text" size="40" id="subject"</td> 
</tr> 
<tr> 
    <td>Description</td> 
    <td> 
     <textarea rows = "5" cols=""50" maxlength="200" id="description"> 
</textarea> 
    </td> 
</tr> 
<tr> <td>Due Date:</td><td><input type ="date" id="duedate"/></td> 
</tr> 
</table> 
<button id = "OK">OK </button><button id = "cancel">Cancel</button> 
<hr/> 
</body> 
</html> 
+0

されますあなたの 'appointments'変数は他の場所にありません – Gerardo

答えて

0

forループはiで増分し、jでは増分する必要があります。現在のものは、無限ループを引き起こしているので、次の行は、「あなたは `appointments.length`を使っているが、私はドンJSエンジンによって処理するためには大きすぎる文字列を作成するので、エラー

table += appt.tableRow(); 
+0

ありがとうございます。これを3時間解決しようとしていました。あなたは最高です。 – kmce

+0

ありがとうございます。親切にも、あなたのために働く答えを受け入れ、あなたを助けたすべての回答を投票してください:) –

関連する問題