Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--suppress ALL -->
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Lab 2</title>
  7. </head>
  8. <body>
  9. <h2>Paragraph no: 1</h2>
  10. <p id="users">
  11.  
  12. </p><br/>
  13. <label>Enter new email: </label><input type="text" id="newEmail"><br/>
  14. <button onclick="changeEmail()">Change Email</button>
  15. <button onclick="showWinner()">Show Winner</button><br/><br/>
  16. <p id="johnny"></p>
  17. <p id="winner"></p>
  18. </body>
  19. <script>
  20. //1.1
  21. function all_properties(obj) {
  22. return Object.getOwnPropertyNames(obj);
  23. }
  24. console.log(all_properties(Array));
  25.  
  26. //1.2
  27. function User(name, email) {
  28. this.Name = name;
  29. this.Email = email;
  30. this.QuizScores = new Array();
  31. this.CurrentScore = 0;
  32. }
  33. User.prototype.constructor=User;
  34. User.prototype.SaveScore=function (TheScoreToAdd) {
  35. this.QuizScores.push(TheScoreToAdd);
  36. this.CurrentScore=TheScoreToAdd;
  37. }
  38. User.prototype.showNameAndScores=function(){
  39. var result="Name "+this.Name+" Scores: ";
  40. if(this.QuizScores.length>0){
  41. for (var i in this.QuizScores){
  42. result+=this.QuizScores[i]+", ";
  43. }
  44. }
  45. else{
  46. result+="No scores yet";
  47. }
  48. return result;
  49. }
  50. User.prototype.ChangeEmail=function(novmail){
  51. this.email=novmail;
  52. }
  53. var u1 = new User("Michael", "michael6@gmail.com");
  54. u1.SaveScore(15);
  55. u1.SaveScore(10);
  56.  
  57. var u2 = new User("Johnny", "johnny@gmail.com");
  58. u2.SaveScore(10);
  59. u2.SaveScore(15);
  60. u2.SaveScore(3);
  61.  
  62. var u3 = new User("Elvis", "TheKing@gmail.com");
  63. var users=[u1, u2, u3];
  64. window.onload=function () {
  65. var users="";
  66. users+=u1.showNameAndScores()+"<br/>"+u2.showNameAndScores()+"<br/>"+u3.showNameAndScores()+"<br/>";
  67. document.getElementById("users").innerHTML=users;
  68. }
  69.  
  70. function changeEmail() {
  71. u2.ChangeEmail(document.getElementById('newEmail').textContent);
  72. document.getElementById('johnny').innerHTML="Johnny’s new email address is "+u2.Email;
  73. }
  74.  
  75. function showWinner() {
  76. var maxPoints=0;
  77. var winner=null;
  78. for(var i in users){
  79. var currentScore=0;
  80. for(j in users[i].QuizScores){
  81. currentScore+=users[i].QuizScores[j];
  82. }
  83. if(currentScore>maxPoints){
  84. maxPoints=currentScore;
  85. winner=users[i];
  86. }
  87. }
  88. document.getElementById('winner').innerHTML="The winner is "+winner.Name+" with "+maxPoints+" points";
  89.  
  90. }
  91. </script>
  92. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement