Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <!--suppress ALL -->
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Lab 2</title>
- </head>
- <body>
- <h2>Paragraph no: 1</h2>
- <p id="users">
- </p><br/>
- <label>Enter new email: </label><input type="text" id="newEmail"><br/>
- <button onclick="changeEmail()">Change Email</button>
- <button onclick="showWinner()">Show Winner</button><br/><br/>
- <p id="johnny"></p>
- <p id="winner"></p>
- </body>
- <script>
- //1.1
- function all_properties(obj) {
- return Object.getOwnPropertyNames(obj);
- }
- console.log(all_properties(Array));
- //1.2
- function User(name, email) {
- this.Name = name;
- this.Email = email;
- this.QuizScores = new Array();
- this.CurrentScore = 0;
- }
- User.prototype.constructor=User;
- User.prototype.SaveScore=function (TheScoreToAdd) {
- this.QuizScores.push(TheScoreToAdd);
- this.CurrentScore=TheScoreToAdd;
- }
- User.prototype.showNameAndScores=function(){
- var result="Name "+this.Name+" Scores: ";
- if(this.QuizScores.length>0){
- for (var i in this.QuizScores){
- result+=this.QuizScores[i]+", ";
- }
- }
- else{
- result+="No scores yet";
- }
- return result;
- }
- User.prototype.ChangeEmail=function(novmail){
- this.email=novmail;
- }
- var u1 = new User("Michael", "michael6@gmail.com");
- u1.SaveScore(15);
- u1.SaveScore(10);
- var u2 = new User("Johnny", "johnny@gmail.com");
- u2.SaveScore(10);
- u2.SaveScore(15);
- u2.SaveScore(3);
- var u3 = new User("Elvis", "TheKing@gmail.com");
- var users=[u1, u2, u3];
- window.onload=function () {
- var users="";
- users+=u1.showNameAndScores()+"<br/>"+u2.showNameAndScores()+"<br/>"+u3.showNameAndScores()+"<br/>";
- document.getElementById("users").innerHTML=users;
- }
- function changeEmail() {
- u2.ChangeEmail(document.getElementById('newEmail').textContent);
- document.getElementById('johnny').innerHTML="Johnny’s new email address is "+u2.Email;
- }
- function showWinner() {
- var maxPoints=0;
- var winner=null;
- for(var i in users){
- var currentScore=0;
- for(j in users[i].QuizScores){
- currentScore+=users[i].QuizScores[j];
- }
- if(currentScore>maxPoints){
- maxPoints=currentScore;
- winner=users[i];
- }
- }
- document.getElementById('winner').innerHTML="The winner is "+winner.Name+" with "+maxPoints+" points";
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement