សំណួរ
១. ទិន្នន័យនៅក្នុងភាសា 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 ។ យើងនឹងសិក្សាពីក្បួនមានស្រាប់ទាំងពីរនេះ នៅពេលខាងមុខទៀត។ មួយវិញទៀត ផ្តើមចេញពីក្បួនមានស្រាប់ទាំងពីរនេះ យើងត្រូវអនុវត្តសរសេរកម្មវិធីដោះស្រាយបញ្ហាដូចខាងក្រោមនេះ៖
- Ask the user for the year of their birth, calculate how old they are and display the result.
- Ask the user for the length of the square side and output the perimeter of that square.
- Ask the user for the circle radius and output the area of such circle.
- 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.
- Implement a currency converter. The user enters dollars, and the program converts them into euros.
- 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.
- 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.
- Implement a calculator. The user enters two numbers, and the program displays the results of + - * / between these numbers.
- 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.
- Employee’s salary is $250 + 10% of sales. Ask for the total amount of sales for a month and calculate the salary