مشروع محوّل وحدات بالجافاسكريبت: الكود كامل + شرح
مشروع 4: محوّل وحدات بجافاسكريبت من الصفر
طول وحرارة، قوائم مترابطة، تحويل فوري، تبديل الاتجاه، ومعالجة الخانة الفاضية والقيم غير الصالحة.
جرّب: 1 كيلومتر = 1000 متر، و0 مئوية = 32 فهرنهايت. الصفر قيمة صحيحة، مش خانة فاضية.
تنبيه: أداة تعليمية وليست أداة قياس معتمدة. التبديل يحافظ على الرقم المكتوب ويعكس الوحدتين.
إيه اللي هتتعلمه في المشروع ده
هتتعلم إزاي تحوّل بين وحدات مختلفة، وإزاي تتعامل مع التحويلات اللي مش مجرد ضرب في رقم زي درجات الحرارة، وتتحقق من المدخلات. وبكده هتفهم ليه التحويلات الحقيقية بتتعمل على خطوات وسيطة بدل ما تبقى كلها معادلة واحدة.
إزاي الكود شغال
الأطوال كلها بتتحوّل عن طريق المتر: value * factor[from] / factor[to]. الحرارة مختلفة لأن الصيغة مش نسبة ثابتة، فالكود بيحوّل الأول لمئوية بدالة toCelsius وبعدين للوحدة المطلوبة بدالة fromCelsius. وفيه تحقق إن القيمة رقم، وإن الطول مش سالب، وإن الحرارة مش أقل من الصفر المطلق. والنتيجة بتتقرّب بـ toPrecision(10) عشان تتجنب أرقام عشرية طويلة.
غلطة شائعة
إنك تستخدم نفس طريقة الضرب مع الحرارة، فتطلع نتايج غلط. غلطة تانية إنك تعرض ناتج عشري خام زي 0.30000000000000004 من غير تقريب.
تمرين تكمل بيه
ضيف الميل والبوصة للأطوال: كل اللي محتاجه سطر في units وسطر في factor (الميل 1609.344 متر والبوصة 0.0254 متر). وبعدها اختبر إن التحويل رايح جاي بيرجّع نفس الرقم.
طريقة التشغيل
- اعمل مجلد جديد وسمّه project4.
- انسخ كل ملف من الأسفل واحفظه بنفس اسمه داخل المجلد.
- افتح index.html في متصفح حديث. أو شغّل خادم محلي من المجلد الذي يحتوي project4:
python3 -m http.server 8000
على ويندوز:py -m http.server 8000
وافتح:http://localhost:8000/project4/
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 • مشروع 04</small><h1>محول الوحدات</h1>
<section><label>نوع التحويل<select id="mode"><option value="length">طول</option><option value="temperature">حرارة</option></select></label>
<label>القيمة<input id="value" type="number" step="any" value="1"></label>
<label>من<select id="from"></select></label><label>إلى<select id="to"></select></label>
<button id="swap">تبديل الاتجاه</button><h2><output id="result"></output></h2>
<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 units = {
length: { m: 'متر', km: 'كيلومتر', cm: 'سنتيمتر' },
temperature: { C: 'مئوية', F: 'فهرنهايت', K: 'كلفن' }
};
const factor = { m: 1, km: 1000, cm: 0.01 };
function options() {
for (const id of ['from', 'to']) {
$(id).replaceChildren();
Object.entries(units[$('mode').value]).forEach(([value, label]) => {
const option = document.createElement('option');
option.value = value; option.textContent = label; $(id).append(option);
});
}
$('to').selectedIndex = 1; convert();
}
function toCelsius(value, unit) {
if (unit === 'F') return (value - 32) * 5 / 9;
if (unit === 'K') return value - 273.15;
return value;
}
function fromCelsius(value, unit) {
if (unit === 'F') return value * 9 / 5 + 32;
if (unit === 'K') return value + 273.15;
return value;
}
function convert() {
const raw = $('value').value.trim();
const value = Number(raw), from = $('from').value, to = $('to').value;
$('status').textContent = '';
if (!raw || !Number.isFinite(value)) { fail('اكتب قيمة رقمية.'); return; }
let result;
if ($('mode').value === 'length') {
if (value < 0) { fail('الطول لا يكون سالبا هنا.'); return; }
result = value * factor[from] / factor[to];
} else {
const celsius = toCelsius(value, from);
if (celsius < -273.15 - 1e-9) { fail('أقل من الصفر المطلق.'); return; }
result = fromCelsius(celsius, to);
}
if (!Number.isFinite(result)) { fail('القيمة أكبر من حدود الحساب.'); return; }
const rounded = Number(result.toPrecision(10));
$('result').textContent = `${Object.is(rounded, -0) ? 0 : rounded} ${units[$('mode').value][to]}`;
}
function fail(message) { $('result').textContent = '—'; $('status').textContent = message; }
$('mode').addEventListener('change', options);
$('value').addEventListener('input', convert);
for (const id of ['from', 'to']) $(id).addEventListener('change', convert);
$('swap').addEventListener('click', () => {
const old = $('from').value; $('from').value = $('to').value; $('to').value = old;
convert();
});
options();
تحميل المشاريع
تحميل ملف ZIP للمشاريع الخمسة (Google Drive)
تابع Ayoub EduTech وطبّق بإيدك.

