www.webdeveloper.com
Results 1 to 2 of 2

Thread: I had .substring and .toUpperCase working, but had to add code and now it's broken?

  1. #1
    Join Date
    Apr 2014
    Posts
    44

    I had .substring and .toUpperCase working, but had to add code and now it's broken?

    So, I had this code working great. But then I realized my assignment was to create a function (in the <head>) that could be called more than once (in the <body>) and would return different results. So, I added some code and now... Well, here's the code:

    PHP Code:
    //in the <head>
    var name
    var capitalLetter
    var smallLetters
    var correctedName
    var firstName
    var lastName
        
        
    function proper(name) {
                var 
    firstLetter name.substring(0,1);
                var 
    theRest name.substring(1);
                
    capitalLetter firstLetter.toUpperCase();
                
    smallLetters theRest.toLowerCase();
                
    correctedName = (firstLetter theRest);
                return 
    correctedName;
                                  }

    //in the <body>
    var name prompt("Please enter your first name. And feel free to type it all wonky (lots of capital letters).""First Name");
    proper(name);
    firstName correctedName;
    name prompt("Please enter your last name. And feel free to type it all wonky (lots of capital letters).""Last Name");
    proper(name);
    lastName correctedName;

    document.write("<p>Your name is " firstName " " lastName ".</p>"); 
    Now it will return the names, but they're still "wonky", as in not corrected to Upper and Lower case letters. If I type "TyLeR DuRdEn" it returns the same. Can someone give me a hand?

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,031
    The main problem is in your proper() function. You are setting correctedName to the firstLetter and theRest variables. Those are the default variables that have not been corrected with toUpperCase() and toLowerCase().

    Also as a side note, since you are using a return value in the function you don't have to set firstName and lastName to correctedName. Instead you can set firstName and lastName to proper(name). And even further optimization would have you put your prompt inside of the proper() function (getting rid of the need for the name variable outside of the function).

    Code:
    // <head> script
    		var name;
    		var capitalLetter;
    		var smallLetters;
    		var correctedName;
    		var firstName;
    		var lastName;
        
    function proper(name) {
    	var firstLetter = name.substring(0,1);
    	var theRest = name.substring(1);
    	capitalLetter = firstLetter.toUpperCase();
    	smallLetters = theRest.toLowerCase();
    	correctedName = (capitalLetter + smallLetters);
    	return correctedName;
    }
    
    // <body> script
    var name = prompt("Please enter your first name. And feel free to type it all wonky (lots of capital letters).", "First Name");
    proper(name);
    firstName = correctedName;
    name = prompt("Please enter your last name. And feel free to type it all wonky (lots of capital letters).", "Last Name");
    proper(name);
    lastName = correctedName;
    
    document.write("<p>Your name is " + firstName + " " + lastName + ".</p>");
    Optimized version:
    Code:
    // <head> script
    function proper(name) {
    	return (name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase());
    }
    
    // <body> script
    var firstName = proper(prompt("Please enter your first name. And feel free to type it all wonky (lots of capital letters).", "First Name"));
    var lastName = proper(prompt("Please enter your last name. And feel free to type it all wonky (lots of capital letters).", "Last Name"));
    
    document.write("<p>Your name is " + firstName + " " + lastName + ".</p>");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles