Section 3.25. substring() practice


3.25. substring() practice

Let's spend a little time practicing how to use the JavaScript substring() method, using the string you saw on the last two pages.

 myString       1Jim   Position:     0    1    2    3    myString.length4You can use myString.length to get the length of a string, which will always be one greater than the last position in the string. myString.substring(1,This substring will start at position 1, end at position 3, and not include the character at position 3. 3); Ji This substring started at position 1 ("J"), and went to position (3 - 1), which is position 2 ("i"). myString.substring(2, myString.length);Since myString.length will be 1 greater than the last position, this will always go to the end of a string. im myString.substring(0,Position 0 is the first character in the string... myString.length);...and myString.length will always return to the end of the string. 1Jim This JavaScript would always return the entire string. 

Just Do It

You should be ready to write the rest of the JavaScript for serveDrink() on your own now. Below is part of that function's JavaScript; your job is to fill in the blanks and get things working.

You'll need to get the response from the server, then break that response into the number of the coffee maker that brewed this order and the name of the person that placed the coffee order. Next you should set the status of the coffee maker that just finished to "Idle" and let the person that ordered coffee know that his order is ready.

 function serveDrink() {   if (request.readyState == ______) {     if (request.status == ______) {        var response = request.responseText;        var whichCoffeemaker = response.substring(___, ___);        var name = response.substring(_____, ________);Here's Where        you can put what you just learned about substring() to use.           if (whichCoffeemaker == "1") {           var coffeemakerStatusDiv1 =             document.getElementById("____________________");           replaceText(_____________________, "Idle");         } else {           var coffeemakerStatusDiv2 =             document.getElementById("____________________");           replaceText(______________________, "_______");         }         ________(name + ", your coffee is ready!");       } else         alert("Error! Request status is " + request.status); coffee.js 





Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net