寫JavaScript遇到難題

本帖最後由 haloPaul 於 2019-2-14 18:45 編輯

以下是用JavaScript建立一個員工資料表,其中粗體的是計算員工的平均薪金,但是我想了很久都不知錯在哪裏。
對於THIS的用法還不是很了解,不知道自己寫得對不對,希望高手指點一下小弟,怎樣寫才能計算員工的平均薪金。
aFullName 是姓名
aAddress是地址
aYearsWorked是工作年期
  1. <html>
  2. <head>
  3. <script>
  4. function EmployeeType(aFullName, aAddress, aYearsWorked)
  5. {
  6. //object properties here
  7.    this.aFullName = aFullName;
  8.    this.aAddress = aAddress;
  9.    this.years = new Array(aYearsWorked);

  10. //Update salary of each year
  11.    this.updateSalary = function(salary, num_Years){
  12.                 this.years[num_Years] = salary;       
  13.                 }

  14. //Calculate average salary
  15.                 this.averageSalary = function() {
  16.                 var total = 0;
  17.                 var avg;
  18.                 for (i = 0; i < num_Years; i++)
  19.                 {
  20.                         total = total + this.years[num_Years];
  21.                 }
  22.                 this.avg = total / num_Years;
  23.                 return this.avg;
  24.         }
  25. }
  26. </script>
  27. </head>

  28. <body>
  29. <script>
  30.         var emp1 = new EmployeeType("David Wong", "kowloon", 3);
  31.         emp1.updateSalary(50000, 0);
  32.         emp1.updateSalary(55000, 1);       
  33.         emp1.updateSalary(60000, 2);       
  34.        document.write(emp1.avg); //show the average salary

  35. </script>
  36. </body>
  37. </html>
複製代碼

以下是用JavaScript建立一個員工資料表,其中粗體的是計算員工的平均薪金,但是我想了很久都不知錯在哪裏。 ...
haloPaul 發表於 2019-2-14 18:43

有冇開 Chrome debugger 睇錯邊行?

via HKEPC IR Pro 3.5.1 - iOS(2.4.1)

TOP

個問題係 avg係local value 唔應該用this, 另外你計算個function averageSalary()冇call 過。

TOP

回覆 1# haloPaul
  1. <html>
  2. <head>
  3. <script>
  4. function EmployeeType(aFullName, aAddress, aYearsWorked)
  5. {
  6. //object properties here
  7. this.aFullName = aFullName;
  8. this.aAddress = aAddress;
  9. this.years = new Array(aYearsWorked);
  10. this.avg = null;
  11. //Update salary of each year
  12. this.updateSalary = function(salary, num_Years){
  13.     this.years[num_Years] = salary;  
  14.     this.averageSalary();
  15.     }

  16. //Calculate average salary
  17. this.averageSalary = function() {
  18.     var total = 0;
  19.     //var avg;
  20.     for (i = 0; i < this.years.length; i++)
  21.     {
  22.       total = total + this.years[i];
  23.     }
  24.     this.avg = total / this.years.length;
  25.     return this.avg;
  26.   }
  27. }
  28. </script>
  29. </head>

  30. <body>
  31. <script>
  32.   var emp1 = new EmployeeType("David Wong", "kowloon", 3);
  33.   emp1.updateSalary(50000, 0);
  34.   emp1.updateSalary(55000, 1);  
  35.   emp1.updateSalary(60000, 2);  
  36. document.write(emp1.avg); //show the average salary

  37. </script>
  38. </body>
  39. </html>
複製代碼

TOP

Thanks a lot. I hope to write a function called display for displaying the employee's details including full name, address, salaries, and the average salary. But it does not work at all, how to solve this problem? I think for a long time and hard to find solutions.
  1. <html>
  2. <head>
  3. <script>


  4. function EmployeeType(aFullName, aAddress, aYearsWorked)
  5. {
  6. //object properties here
  7. this.aFullName = aFullName;
  8. this.aAddress = aAddress;
  9. this.years = new Array(aYearsWorked);
  10. this.avg = null;
  11. //Update salary of each year
  12. this.updateSalary = function(salary, num_Years){
  13.     this.years[num_Years] = salary;  
  14.     this.averageSalary();
  15.        
  16.     }

  17. //Calculate average salary
  18. this.averageSalary = function() {
  19.     var total = 0;
  20.     //var avg;
  21.     for (i = 0; i < this.years.length; i++)
  22.     {
  23.       total = total + this.years[i];
  24.     }
  25.     this.avg = total / this.years.length;
  26.     return this.avg;
  27.   }
  28.   
  29.   //Show the result
  30. this.display = function() {
  31. return emp1.aFullName;
  32. return emp1.aAddress;
  33. }
  34. }

  35. </script>
  36. </head>

  37. <body>
  38. <script>
  39. var emp1 = new EmployeeType("David Wong", "kowloon", 3);
  40.   emp1.updateSalary(50000, 0);
  41.   emp1.updateSalary(55000, 1);  
  42.   emp1.updateSalary(60000, 2);  
  43.   document.write(emp1.avg); //show the average salary
  44.   emp1.display();
  45. </script>
  46. </body>
  47. </html>
複製代碼

TOP

本帖最後由 marlin12 於 2019-3-2 11:42 編輯

你可以用JSON.stringify去把object裏全部既properties變成一個JSON字串,然後寫到HTML裏。
show object properties - codepen
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.css" />
  5. </head>

  6. <body>
  7.   <div id="result"></div>

  8.   <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  9.   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.js"></script>

  10.   <script>
  11.     function Employee(_fullName, _address) {
  12.       this.fullName = _fullName;
  13.       this.address = _address;
  14.       this.salaries = [];
  15.       this.averageSalary = 0;

  16.       this.addSalary = function(_year, _amount) {
  17.         this.salaries.push({
  18.           year: _year,
  19.           amount: _amount
  20.         });

  21.         this.averageSalary = average( this.salaries.map( item => item.amount ) );
  22.       }

  23.       function average(array) {
  24.         return array.reduce( (acc, elem) => (acc + elem), 0 ) / array.length;
  25.       }
  26.     };

  27.     let emp1 = new Employee( "David Wong", "Kowloon" );
  28.     emp1.addSalary(2016, 50000);
  29.     emp1.addSalary(2017, 55000);
  30.     emp1.addSalary(2018, 60000);

  31.     $("#result").JSONView( JSON.stringify(emp1) );
  32.   </script>
  33. </body>
  34. </html>
複製代碼
你可以到這裏學習怎樣編寫JavaScript。
JavaScript Tutorial - w3school.com
另外,你的英文也需要[惡補]一下。

TOP

睇呢2行
//var avg;
return this.avg;

同埋唔好乜都this.啦

TOP