مشروع دفتر المصاريف بالجافاسكريبت: الكود كامل + شرح
مشروع 1: دفتر المصاريف بجافاسكريبت من الصفر
إضافة وحذف، حساب الإجمالي بالقروش، تحقق من المبلغ، وحفظ محلي مع التعامل مع فشل التخزين.
جرّب: ضيف 35.50 و20، احذف بند، واعمل ريلود. وجرّب الصفر والخانة الفاضية.
تنبيه: الحفظ خاص بنفس المتصفح والموقع، مش سجل مالي رسمي ولا نسخة احتياطية.
إيه اللي هتتعلمه في المشروع ده
المشروع ده بيعلمك تتعامل مع فورم، وتحفظ بياناتك في localStorage، وتحسب إجمالي من غير أخطاء الكسور العشرية. هتشوف كمان إزاي ترسم قائمة من مصفوفة وتحذف منها بند.
إزاي الكود شغال
المبلغ بيتحوّل لقروش صحيحة بـ Math.round(number * 100) ويتخزن في cents، والإجمالي بيتحسب بـ reduce على القروش وبعدين بيتقسم على 100 وقت العرض بس. أي تغيير بيعدّي على دالة update اللي بتعيد الرسم وتحفظ جوه try/catch، فلو التخزين مش متاح بتظهر رسالة والتغيير يفضل في الذاكرة. وعند فتح الصفحة الكود بيتأكد إن المحفوظ مصفوفة وكل بند فيه وصف ومبلغ سليم قبل ما يستخدمه.
غلطة شائعة
إنك تخزن المبلغ كرقم عشري وتجمعهم زي ما هما، لأن 0.1 + 0.2 مش بالظبط 0.3. وغلطة تانية إنك تثق في اللي موجود في localStorage من غير تحقق، ولو اتعدّل أو باظ الصفحة كلها تقف. هنا الكود بيبدأ بقائمة فاضية ويقولك لو لقى بيانات تالفة.
تمرين تكمل بيه
ضيف خانة تاريخ لكل مصروف وخزّنها مع البند، وبعدين اعرض القائمة من الأحدث للأقدم. ولما تخلص، جرّب تضيف زرار تعديل للبند.
طريقة التشغيل
- اعمل مجلد جديد وسمّه project1.
- انسخ كل ملف من الأسفل واحفظه بنفس اسمه داخل المجلد.
- افتح index.html في متصفح حديث. أو شغّل خادم محلي من المجلد الذي يحتوي project1:
python3 -m http.server 8000
على ويندوز:py -m http.server 8000
وافتح:http://localhost:8000/project1/
index.html
<!doctype html>
<html lang="ar" dir="rtl"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>دفتر المصاريف | Ayoub EduTech</title><link rel="stylesheet" href="style.css">
<script src="app.js" defer></script></head><body><main>
<small>Ayoub EduTech • مشروع 01</small><h1>دفتر المصاريف</h1>
<section><form id="form" novalidate>
<label>وصف المصروف<input id="description" maxlength="60" required></label>
<label>المبلغ بالجنيه<input id="amount" type="number" min="0.01" step="0.01" required></label>
<button>إضافة مصروف</button></form></section>
<section><div class="row"><h2>الإجمالي</h2><output id="total">0.00 ج.م</output></div>
<ul id="list"></ul><small>الحفظ على هذا المتصفح فقط، وليس سجلا ماليا رسميا.</small></section>
<p id="status" role="status" aria-live="polite"></p>
<small>مشروع تعليمي محلي • JavaScript</small></main></body></html>
style.css
* {
box-sizing:border-box
}
body {
margin:0;
background:#140b26;
color:#f7f2ff;
font:18px "Noto Sans Arabic",Arial;
direction:rtl
}
main {
max-width:740px;
margin:32px auto;
padding:24px
}
h1 {
font-size:30px;
margin:8px 0
}
small {
color:#c5b2de
}
section,article {
background:#24133d;
border:1px solid #614480;
border-radius:18px;
padding:20px;
margin:18px 0
}
label {
display:block;
margin:12px 0
}
input,select,button {
font:inherit;
border-radius:10px;
padding:10px;
border:1px solid #8867a7
}
input,select {
background:#10091b;
color:white;
width:100%
}
button {
background:#f5ca52;
color:#211234;
cursor:pointer;
margin:5px
}
button:disabled {
opacity:.45;
cursor:not-allowed
}
button:focus-visible,input:focus-visible,select:focus-visible {
outline:3px solid #68e1d2
}
output,strong {
color:#7ce6d5
}
.row {
display:flex;
gap:10px;
align-items:center;
justify-content:space-between
}
.row>* {
min-width:0
}
ul {
padding:0;
list-style:none
}
li {
padding:12px 0;
border-bottom:1px solid #59416c
}
.error {
color:#ffb6bb
}
#status {
min-height:32px
}
#clock {
font:64px monospace;
text-align:center;
direction:ltr
}
#choices button {
display:block;
width:100%;
text-align:right
}
#choices button.correct {
background:#68d7b8
}
#choices button.wrong {
background:#ee9ca9
}
#products article {
margin:10px 0
}
a {
color:#7ce6d5
}
@media(max-width:500px) {
main {
padding:16px;
margin:10px auto
}
.row {
flex-wrap:wrap
}
h1 {
font-size:26px
}
}
app.js
const $ = id => document.getElementById(id);
const key = 'ae-expenses-v1';
let entries = [];
try {
const data = JSON.parse(localStorage.getItem(key) ?? '[]');
if (Array.isArray(data) && data.every(x => x &&
typeof x.text === 'string' && x.text.trim() &&
Number.isSafeInteger(x.cents) && x.cents > 0)) entries = data;
} catch { $('status').textContent = 'تعذرت قراءة المحفوظ. بدأنا بقائمة فارغة.'; }
function render() {
$('list').replaceChildren();
entries.forEach((entry, index) => {
const li = document.createElement('li');
const label = document.createElement('span');
label.textContent = `${entry.text}: ${(entry.cents / 100).toFixed(2)} ج.م`;
const button = document.createElement('button');
button.textContent = 'حذف';
button.addEventListener('click', () => {
entries.splice(index, 1); update();
});
li.append(label, button); $('list').append(li);
});
const sum = entries.reduce((total, x) => total + x.cents, 0);
$('total').textContent = `${(sum / 100).toFixed(2)} ج.م`;
if (!entries.length) $('list').textContent = 'لا توجد مصاريف بعد.';
}
function update() {
render();
try {
localStorage.setItem(key, JSON.stringify(entries));
$('status').textContent = 'تم الحفظ على هذا المتصفح.';
} catch { $('status').textContent = 'الحفظ غير متاح. التغيير في الذاكرة فقط.'; }
}
$('form').addEventListener('submit', event => {
event.preventDefault();
const text = $('description').value.trim();
const raw = $('amount').value.trim();
const number = Number(raw);
if (!text || !/^\d+(\.\d{1,2})?$/.test(raw) ||
!Number.isFinite(number) || number <= 0 || number > 1000000) {
$('status').textContent = 'اكتب وصفا ومبلغا أكبر من صفر، بحد أقصى مليون ومنزلتين عشريتين.';
return;
}
entries.push({ text, cents: Math.round(number * 100) });
update(); $('form').reset(); $('description').focus();
});
render();
تحميل المشاريع
تحميل ملف ZIP للمشاريع الخمسة (Google Drive)
تابع Ayoub EduTech وطبّق بإيدك.

