├── Building a Cash Register.js ├── Building an Address Book.js ├── Code your own Adventure.js ├── Contact List.js ├── Dragon Slayer.js ├── My First Game.js ├── README.md └── Rock-Paper-Scissors Game.js /Building a Cash Register.js: -------------------------------------------------------------------------------- 1 | function StaffMember(name,discountPercent){ 2 | this.name = name; 3 | this.discountPercent = discountPercent; 4 | } 5 | 6 | var sally = new StaffMember("Sally",5); 7 | var bob = new StaffMember("Bob",10); 8 | 9 | // Create yourself again as 'me' with a staff discount of 20% 10 | 11 | 12 | var cashRegister = { 13 | total:0, 14 | lastTransactionAmount: 0, 15 | add: function(itemCost){ 16 | this.total += (itemCost || 0); 17 | this.lastTransactionAmount = itemCost; 18 | }, 19 | scan: function(item,quantity){ 20 | switch (item){ 21 | case "eggs": this.add(0.98 * quantity); break; 22 | case "milk": this.add(1.23 * quantity); break; 23 | case "magazine": this.add(4.99 * quantity); break; 24 | case "chocolate": this.add(0.45 * quantity); break; 25 | } 26 | return true; 27 | }, 28 | voidLastTransaction : function(){ 29 | this.total -= this.lastTransactionAmount; 30 | this.lastTransactionAmount = 0; 31 | }, 32 | // Create a new method applyStaffDiscount here 33 | 34 | 35 | }; 36 | 37 | cashRegister.scan('eggs',1); 38 | cashRegister.scan('milk',1); 39 | cashRegister.scan('magazine',3); 40 | // Apply your staff discount by passing the 'me' object 41 | // to applyStaffDiscount 42 | 43 | 44 | // Show the total bill 45 | console.log('Your bill is '+cashRegister.total.toFixed(2)); -------------------------------------------------------------------------------- /Building an Address Book.js: -------------------------------------------------------------------------------- 1 | var bob = { 2 | firstName: "Bob", 3 | lastName: "Jones", 4 | phoneNumber: "(650) 777-7777", 5 | email: "bob.jones@example.com" 6 | }; 7 | 8 | var mary = { 9 | firstName: "Mary", 10 | lastName: "Johnson", 11 | phoneNumber: "(650) 888-8888", 12 | email: "mary.johnson@example.com" 13 | }; 14 | 15 | var contacts = [bob, mary]; 16 | 17 | function printPerson(person) { 18 | console.log(person.firstName + " " + person.lastName); 19 | } 20 | 21 | function list() { 22 | var contactsLength = contacts.length; 23 | for (var i = 0; i < contactsLength; i++) { 24 | printPerson(contacts[i]); 25 | } 26 | } 27 | 28 | /*Create a search function 29 | then call it passing "Jones"*/ 30 | var search = function(lastName) { 31 | var contactsLength = contacts.length; 32 | for (var i = 0; i < contactsLength; i++) { 33 | if(lastName === contacts[i].lastName) { 34 | printPerson(contacts[i]); 35 | } 36 | } 37 | }; 38 | 39 | var add = function(firstName,lastName,email,phoneNumber) { 40 | var newFriend = new Object(); 41 | newFriend.firstName = firstName; 42 | newFriend.lastName = lastName; 43 | newFriend.email = email; 44 | newFriend.phoneNumber = phoneNumber; 45 | contacts[contacts.length] = newFriend; 46 | }; 47 | 48 | add("Gourav","Suri","gouravsuri@gmail.com","1234567890"); 49 | list(); -------------------------------------------------------------------------------- /Code your own Adventure.js: -------------------------------------------------------------------------------- 1 | var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase(); 2 | 3 | switch(troll) { 4 | case 'FIGHT': 5 | var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase(); 6 | var smart = prompt("Are you smart?").toUpperCase(); 7 | if(strong === 'YES' || smart === 'YES') { 8 | console.log("You only need one of the two! You beat the troll--nice work!"); 9 | } else { 10 | console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!"); 11 | } 12 | break; 13 | case 'PAY': 14 | var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase(); 15 | var dollars = prompt("Is your money in Troll Dollars?").toUpperCase(); 16 | if(money === 'YES' && dollars === 'YES') { 17 | console.log("Great! You pay the troll and continue on your merry way."); 18 | } else { 19 | console.log("Dang! This troll only takes Troll Dollars. You get whomped!"); 20 | } 21 | break; 22 | case 'RUN': 23 | var fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase(); 24 | var headStart = prompt("Did you get a head start?").toUpperCase(); 25 | if(fast === 'YES' || headStart === 'YES') { 26 | console.log("You got away--barely! You live to stroll through the forest another day."); 27 | } else { 28 | console.log("You're not fast and you didn't get a head start? You never had a chance! The troll eats you."); 29 | } 30 | break; 31 | default: 32 | console.log("I didn't understand your choice. Hit Run and try again, this time picking FIGHT, PAY, or RUN!"); 33 | } -------------------------------------------------------------------------------- /Contact List.js: -------------------------------------------------------------------------------- 1 | var friends = new Object(); 2 | friends.bill = { 3 | firstName: "Bill", 4 | lastName: "Gomez", 5 | number: "1234567890", 6 | address: ['One Microsoft Way','Redmond','WA','98052'] 7 | }; 8 | friends.steve = { 9 | firstName: "Steve", 10 | lastName: "Henrie", 11 | number: "0987654321", 12 | address: ['C-18','Delhi','New Delhi','1100'] 13 | }; 14 | 15 | var list = function(friends) { 16 | for(var key in friends) { 17 | console.log(key); 18 | } 19 | }; 20 | 21 | var search = function(name) { 22 | for(var key in friends) { 23 | if(friends[key].firstName === name) { 24 | console.log(friends[key]); 25 | return friends[key]; 26 | } 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Dragon Slayer.js: -------------------------------------------------------------------------------- 1 | var slaying = true; 2 | var youHit = Math.floor(Math.random()*2); // value will be ) or 1 3 | var damageThisRound = Math.floor(Math.random()*5 + 1); //value will be from 1 to 5 4 | var totalDamage = 0; 5 | 6 | while(slaying) 7 | { 8 | if(youHit) { 9 | console.log("Congrats, You hit the dragon!"); 10 | totalDamage += damageThisRound; 11 | if(totalDamage >=4 ) { 12 | console.log("You slew the dragon"); 13 | slaying = false; 14 | } else { 15 | youHit = Math.floor(Math.random()*2); 16 | } 17 | }else { 18 | console.log("Dragon defeated you!"); 19 | slaying = false; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /My First Game.js: -------------------------------------------------------------------------------- 1 | // Check if the user is ready to play! 2 | confirm("Are you ready to bang ON?"); 3 | 4 | var age = prompt("What's your age"); 5 | 6 | if(age<13) 7 | { 8 | console.log("Allowed to play but if something goes wrong, you lilly will be in trouble"); 9 | } 10 | else { 11 | console.log("BAng ON"); 12 | } 13 | 14 | console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'") 15 | 16 | console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'"); 17 | 18 | var userAnswer = prompt("Do you want to race Bieber on stage?"); 19 | 20 | if(userAnswer === "yes") 21 | { 22 | console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!"); 23 | } 24 | else 25 | { 26 | console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"); 27 | } 28 | var feedback = prompt("Rate my game out of 10"); 29 | if(feedback>8) 30 | { 31 | console.log("Thank you! We should race at the next concert!"); 32 | } 33 | else 34 | { 35 | console.log("I'll keep practicing coding and racing."); 36 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Javascript-Mini-Projects 2 | These projects are developed by me while pursuing Codecademy Javascript course for beginners 3 | -------------------------------------------------------------------------------- /Rock-Paper-Scissors Game.js: -------------------------------------------------------------------------------- 1 | var userChoice = prompt("Do you choose rock, paper or scissors?"); 2 | var computerChoice = Math.random(); 3 | if (computerChoice < 0.34) { 4 | computerChoice = "rock"; 5 | } else if(computerChoice <= 0.67) { 6 | computerChoice = "paper"; 7 | } else { 8 | computerChoice = "scissors"; 9 | } console.log("Computer: " + computerChoice); 10 | compare(userChoice,computerChoice); 11 | 12 | var compare = function(choice1,choice2) { 13 | if(choice1 === choice2) { 14 | return "The result is a tie!" ; 15 | }else if(choice1 === "rock") { 16 | if(choice2 === "scissors") { 17 | return "rock wins"; 18 | }else { 19 | return "paper wins"; 20 | } 21 | }else if(choice1 === "paper") { 22 | if(choice2 === "rock") { 23 | return "paper wins"; 24 | }else { 25 | return "scissors wins"; 26 | } 27 | }else if(choice1 === "scissors") { 28 | if(choice2 === "rock") { 29 | return "rock wins"; 30 | }else { 31 | return "scissors wins"; 32 | } 33 | } 34 | }; 35 | --------------------------------------------------------------------------------