លំហាត់​អនុវត្តន៍
សំណួរ

១. ទិន្នន័យ​នៅ​ក្នុង​ភាសា JS ត្រូវ​គេ​ចែក​ចេញ​ជា ២ ក្រុម​គឺ ក្រុម​ដែល​ជា​ទិន្នន័យ​ទោល ឬ ទិន្នន័យ​ដើម (primitive data) និង​ក្រុម​ដែល​ជា​ទិន្នន័យ​ចំរុះ ឬ ទិន្នន័យ​សមាស (compound data) ដែល​កើត​ឡើង​ពី​ការយក​ទិន្នន័យ​ទោល​មួយ​ចំនួន​មក​ដាក់​ជាមួយ​គ្នា​។ តើ​ទិន្នន័យ​ដើម​មាន​អ្វី​ខ្លះ និង ទិន្នន័យ​សមាស​មាន​អ្វី​ខ្លះ​?
២. អ្វី​ទៅ​ហៅ​ថា​អថេរ​?
៣. តើ​សញ្ញាស្មើ (=) នៅ​ក្នុង​ភាសា JS មាន​តួនាទី​ដូចម្តេច​ដែរ?
៤. តើ​តក្កអថេរ​ជាអ្វី? តើ​ប្រមាណ​វិធី​អ្វី​ខ្លះ​ដែល​ផ្តល់​លទ្ធផល​ជា​តក្កអថេរ?
៥. តើ​តំលៃ​អ្វី​ខ្លះ​ដែល​សមមូល​នឹង false?

លំហាត់អនុវត្តន៍

នៅ​ក្នុង​ភាសា JS ដើម្បី​ទទួល​យក​ព័ត៌មាន​ឬ​ទិន្នន័យ​ពី​អ្នក​ប្រើប្រាស់ យើង​អាច​ប្រើប្រាស់​ផ្ទាំង dialog ម៉្យាង​សំរាប់​ទទួល​យក​ទិន្នន័យ​ឬ​ព័ត៌មាន​នោះ​។ ហើយ​ដើម្បី​បង្ហាញ​ព័ត៌មាន​ឬ​ទិន្នន័យ​ដល់​អ្នក​ប្រើប្រាស់វិញ យើង​អាច​ប្រើប្រាស់​ផ្ទាំង dialog មួយ​បែប​ទៀត​។ ពិនិត្យ​កម្មវិធី​ខាង​ក្រោម​នេះ៖
var name = prompt("សូម​សរសេរ​ឈ្មោះ​របស់​អ្នក​នៅ​ខាង​ក្រោម​នេះ៖");
alert("សួស្តី "+name+" សុខ​សប្បាយ​ជា​ទេ​ថ្ងៃ​នេះ?");
នៅ​លើ​បន្ទាត់​លេខ 1 គឺ​ជាការ​បង្កើត​ផ្ទាំង dialog មួយ​សំរាប់​ទទួល​យក​ទិន្នន័យ​ដែល​ជា​ឈ្មោះ​របស់​អ្នក​ប្រើប្រាស់​។ ហើយ​ឈ្មោះ​នោះ​ត្រូវ​បាន​ភ្ជាប់​ទៅ​នឹង​អថេរ name ទុក​សំរាប់​យក​ទៅ​ប្រើប្រាស់​នៅ​កន្លែង​ផ្សេង​ទៀត​នៅ​ក្នុង​កម្មវិធី​។

ចំណែក​ឯ​នៅ​លើ​បន្ទាត់​លេខ 2 វីញ គឺ​ជា​ការបង្កើត​ផ្ទាំង dialog មួយ​ប្រភេទ​ទៀត ដើម្បី​យក​ទិន្នន័យ​ដែល​ជា​ឈ្មោះ​របស់​អ្នក​ប្រើប្រាស់​នោះ មក​សរសេរ​បង្ហាញ​នៅ​លើ​ផ្ទាំង dialog នោះ​។

តាមការពិត ពាក្យ​ថា prompt និង alert គឺ​ជា​ក្បួន​មាន​ស្រាប់ (built-in function) ​នៅ​ក្នង​ភាសា JS ។ យើង​នឹង​សិក្សា​ពី​ក្បួន​មាន​ស្រាប់​ទាំងពីរ​នេះ​ នៅ​ពេល​ខាង​មុខ​ទៀត​។ មួយវិញទៀត ផ្តើម​ចេញ​ពី​ក្បួន​មាន​ស្រាប់​ទាំងពីរ​នេះ យើង​ត្រូវ​អនុវត្ត​សរសេរ​កម្មវិធី​ដោះ​ស្រាយ​បញ្ហា​ដូច​ខាង​ក្រោម​នេះ៖
  1. Ask the user for the year of their birth, calculate how old they are and display the result.
  2. Ask the user for the length of the square side and output the perimeter of that square.
  3. Ask the user for the circle radius and output the area of such circle.
  4. Ask the user for distance in km between two cities and in how many hours they want to get there. Calculate the required speed to do it on time.
  5. Implement a currency converter. The user enters dollars, and the program converts them into euros.
  6. The user enters amount of money in their wallet and the price of one chocolate bar. The program shows how many chocolate bars a user can buy and how much change they will have.
  7. Implement a converter from kilometers to miles (the user enters kilometers, and the program displays miles). 1km = 0.621371miles. Specify this value in the code as a constant.
  8. Implement a calculator. The user enters two numbers, and the program displays the results of + - * / between these numbers.
  9. The user enters values of a and b for the formula a * x + b = 0, and the program calculates and displays the value of x.
  10. Employee’s salary is $250 + 10% of sales. Ask for the total amount of sales for a month and calculate the salary