مقدمه
در این جلسه از آموزش جاوا اسکریپت، بهزاد فرهادی بر موضوعات زیر تمرکز دارد:
مرور متغیرهای محلی و سراسری
کار با ساختارهای شرطی
حل تمرینهای فصل ۷
نوشتن کدهای تعاملی در مرورگر با دستور
prompt
تبدیل رشته به عدد با تابع
Number()
در طول جلسه، او با حل تمرینات و مثالهای عملی، این مفاهیم را توضیح میدهد.
محتوای اصلی
متغیرهای سراسری و محلی
بهزاد با توضیح متغیرهای سراسری (global) و محلی (local) شروع میکند. او از یک مثال برای معرفی این مفهوم استفاده میکند که در آن یک متغیر سراسری به نام totalScore
با مقدار اولیه 0
تعریف شده است. این متغیر در تمام توابع در دسترس است.
نمونه کد:
let totalScore = 0; // متغیر سراسری
سپس، تابع answerQuestion(points)
تعریف میشود که:
یک پارامتر به نام
points
دریافت میکند.اگر مقدار
points
بیشتر از0
باشد، آن را بهtotalScore
اضافه میکند.در غیر این صورت،
"No score added"
را چاپ میکند.
function answerQuestion(points) {
if (points > 0) {
totalScore += points;
} else {
console.log("No score added");
}
}
فراخوانی تابع و خروجیها
برای نمایش تأثیر تابع بر مقدار متغیر سراسری، مقدار totalScore
قبل و بعد از اجرای تابع در console.log
نمایش داده میشود:
نمونه اجرا:
console.log("Before:", totalScore); // مقدار اولیه: 0
answerQuestion(10);
console.log("After:", totalScore); // مقدار جدید: 10
اگر تابع answerQuestion(5)
دوباره اجرا شود، مقدار totalScore
به 15 افزایش مییابد که نشان میدهد توابع میتوانند مقدار متغیرهای سراسری را تغییر دهند.
ساختارهای شرطی (تمرین ۴)
بهزاد یک تمرین معرفی میکند که در آن، یک تابع به نام recommendMeal(time)
بر اساس مقدار زمان ورودی یک وعده غذایی را پیشنهاد میدهد.
پیادهسازی:
function recommendMeal(time) {
if (time <= 10) {
console.log("Breakfast");
} else if (time <= 15) {
console.log("Lunch");
} else if (time <= 20) {
console.log("Dinner");
} else {
console.log("Snack Time");
}
}
زمان ≤ ۱۰ →
"صبحانه"
۱۱ تا ۱۵ →
"ناهار"
۱۶ تا ۲۰ →
"شام"
> ۲۰ →
"زمان میانوعده"
نتیجهگیری
در این جلسه، بهزاد آموزش داد که چگونه:
متغیرهای سراسری و محلی – و تأثیر آنها در برنامهنویسی را درک کنیم.
ساختارهای شرطی – برای تصمیمگیری منطقی از
if-else
استفاده کنیم.کدهای تعاملی – نحوه نوشتن توابعی که مقدار متغیرها را تغییر میدهند.
حل تمرینات عملی – مانند سیستم امتیازدهی و پیشنهاد وعدههای غذایی.
در پایان جلسه، دانشجویان باید درک بهتری از دامنه متغیرها، عبارات شرطی و تعامل توابع در جاوا اسکریپت داشته باشند.
Summary of “JavaScript by Behzad Farhadi – Session 9”
Introduction
In this session of JavaScript training, Behzad Farhadi focuses on several key topics, including:
Reviewing local and global variables
Working with conditional structures
Solving exercises from Chapter 7
Writing interactive code in the browser using
prompt
Converting strings to numbers with the
Number()
function
The session includes hands-on exercises and explanations to reinforce these concepts.
Main Content
Global and Local Variables
Behzad begins by explaining global and local variables. He introduces the concept using an example where a global variable named totalScore
is initialized at 0
. This variable is accessible across different functions.
Example Code:
let totalScore = 0; // Global variable
A function answerQuestion(points)
is defined, which:
Accepts a
points
parameterIf
points > 0
, adds it tototalScore
Otherwise, logs
"No score added"
Implementation:
function answerQuestion(points) {
if (points > 0) {
totalScore += points;
} else {
console.log("No score added");
}
}
Function Invocation and Output
To demonstrate how functions modify global variables, Behzad calls console.log
before and after invoking the function:
Example Execution:
console.log("Before:", totalScore); // Expected: 0
answerQuestion(10);
console.log("After:", totalScore); // Expected: 10
Conditional Structures (Exercise 4)
Behzad introduces an exercise where a function named recommendMeal(time)
recommends a meal based on the given time of day.
Implementation:
function recommendMeal(time) {
if (time <= 10) {
console.log("Breakfast");
} else if (time <= 15) {
console.log("Lunch");
} else if (time <= 20) {
console.log("Dinner");
} else {
console.log("Snack Time");
}
}
Time ≤ 10 → "Breakfast"
11 to 15 → "Lunch"
16 to 20 → "Dinner"
> 20 → "Snack Time"
Conclusion
In this session, Behzad teaches:
Global vs. Local Variables – How functions interact with them.
Conditional Structures – How to use
if-else
for decision-making.Interactive Coding – Writing functions that modify variables based on input.
Practical Examples – Solving real-world problems like scoring systems and meal recommendations.
By the end of the lesson, students should be comfortable handling variable scope, conditional statements, and basic function interactions in JavaScript.
