• Lets play a game
    3 replies, posted
So lets play a game. Who can debug this code? It is in Javascript. Have fun. /** * ################################################################# # ALFAVA METRAXIS # # # # # # # # YOUR NAME # # # # # # # # PHONE NUMBER EMAIL ADDRESS # # # # # ################################################################# # This program works out the cost of new business cards for the user. # The base cost of the cards is £2 for 50. However users can customise their cards at additional cost */ //This line allows us to use command line input and output var io = require('./io'); // Creating a variable to hold the total cost of their order in £ // Print an introduction out to the user io.output("Welcome to Alfava Metraxis (TM). This program will help you design your perfect business card."); // Before we print out this line we'll add a newline for prettiness io.output("You get the first 8 characters free. After that it is 10p per character for the next 10 characters and 2p per character thereafter"); // Their name goes across the middle of the card. We'll ask them for it and store it in a variable var theirName; theirName = io.input("What is your name? "); // We'll confirm their name as it's so important if(io.input("Is your name ", theirName + "?") == "yes") { // They entered their name wrong and we need to change it. Good job we checked! theirName = io.input("What is your name? "); } /* * The card cost goes up based on the length of their name. Your £2 gets you 8 characters (excluding spaces). * Thereafter it's 10p per character for the next 10 characters and 2p for every character thereafter. */ /* We need the length of their name to know how much to charge them */ var nameLength; nameLength = theirName.length(); // Given that length, what's the cost if(nameLength > 8 && nameLength < 18) { // Add 10p per character totalCost += (nameLength - 8) * 10; } if(nameLength > 18) { // Add 2p per character totalCost += (nameLength - 18) * 2; } // Let's inform the user of how much *extra* this is going to cost them. I've formatted it nicely as well io.output("The length of your name is", length, "which will cost an additional £" + parseFloat(totalCost).toFixed(2)); //-----------------------------------------// /* # On their card they can have an email address AND/OR a phone number. # They get only one of these included the basic cost. # If they want both it costs another £1. # If they want neither they can save 50p */ // Ask if they want an email address on their card var emailWanted = io.input("Do you want an email address? ", theirName + "?"); if(emailWanted) { // If they do then ask them what it should be var emailAddress = io.input("What is your email address? ", theirName + "?"); } // Ask if they want an phone number on their card var phoneNumberWanted = io.input("Do you want a phone number? ", theirName + "?"); if(phoneNumberWanted) { // If they do then ask them what it should be phoneNumber = io.input("What is your phone number? ", theirName + "?") } // Add £1 to the total cost if they want both if(phoneNumberWanted && emailWanted); { totalCost += 1; // Take 50p off if they don't want either if(!phoneNumberWanted || !emailWanted) { totalCost -= 0.5; } } /* # They can also have the corners of their cards curved (at extra cost of course) # If they want an even number curved then they pay an additional £3 divided by the number of curves # If they want an odd number curved then they pay an additional £5 divided by the number of curves # This does mean that if they want more curves it costs less - it's just a quirk of the manufacturing process! */ // Find out how many corners they would like curved var numberOfCurves = io.input("How many curved corners would you like? ", theirName + "?"); // This line tells us whether we have an even number of curves or an odd one. if(numberOfCurves % 2 == 1) { // Even number - add on cost totalCost += 3 / numberOfCurves; } else { // Odd number - add on cost totalCost += 5 / numberOfCurves } //-----------------------------------------// /* We are currently running a deal for them - they can order both shiny and matte cards and we'll give them the lower number of cards for free */ // How many orders do they need? io.output("\nYou can order both shiny and matte cards and we'll give you the lower number free of charge") var smallerNumber = parseInt(io.input("How many shiny cards would you like? ", theirName + "?")); var biggerNumber = parseInt(io.input("How many matte cards would you like? ", theirName + "?")); // If they want more shiny cards then just swap these round if (biggerNumber < smallerNumber) { smallerNumber = biggerNumber; biggerNumber = smallerNumber; } // Let them know how many free cards they're getting! io.output("You will get", smallerNumber, "free copies! Bargain eh!\n"); //-----------------------------------------// //***************************************************// //The order is now complete - let's summarise what we're giving them //***************************************************// io.output("Your order summary\n-----------"); io.output("The name is:", theirName); if(phoneNumberWanted) { io.output("The phone number is", phoneNumber); } if(emailWanted) { io.output("The email address is", emailWanted); } io.output("Number of curved corners is", numberOfCurves); /* * We need some way to contact them * They may have given us a phone number or an email address already, wo let's use that. * But if they've not given us either, OR they've given us both we'll have to ask * */ if(phoneNumberWanted) { if(emailWanted) { //Both email and phone number around - ask them which one contactDetails = io.input("Give me a phone number or email address to contact you on ", theirName + "?"); } if(!emailWanted) { if (!phoneNumberWanted) { // No email and no phone number - let's ask contactDetails = io.input("Give me a phone number or email address to contact you on ", theirName + "?"); } else if (phoneNumberWanted) { // We have no email but we do have a phone contactDetails = phoneNumber; } }} else if(!phoneNumberWanted) { if (emailWanted) { // No phone but do have email contactDetails = emailAddress; } else { // no email and no phone - ask contactDetails = io.input("Give me a phone number or email address to contact you on ", theirName + "?"); } } else { // Don't have anything - just ask them contactDetails = io.input("Give me a phone number or email address to contact you on ", theirName + "?"); } io.output("OK, your order has gone through and we will contact you on", contactDetails); io.output("Goodbye");
no
It's alright now I think I am getting through it lol
Sorry, you need to Log In to post a reply to this thread.