Preview

09 - String Manipulation

 1. A big part of your life as a programmer is manipulating strings of text. To help you with this, Javascript comes with lots of _______________ you can use

  inbuilt methods

  variables

  internal variables

  external generators

 2. If you need to change the case of a string of text you can use the two inbuilt methods toUpperCase and ________________

  CaseLower

  Lower1

  toLowerCase

  toCase

 3. The first line sets up a variable called postcode. To the right of the equal sign you type the ________________________
<SCRIPT LANGUAGE = "Javascript">

var postcode = "sr2 4jh";
var new_postcode = postcode.toUpperCase();

document.write( new_postcode );

</SCRIPT>

  variable you want to convert

  method you wish to convert

  function you wish to call

  variable you wish to delete

 4. The newly converted value will be placed in the variable we've called new_postcode. The reason that we're storing the converted value into a new variable is because in Javascript strings are immutable. This means __________
<SCRIPT LANGUAGE = "Javascript">

var postcode = "sr2 4jh";
var new_postcode = postcode.toUpperCase();

document.write( new_postcode );

</SCRIPT>

  that you cannot actually convert a string directly to anything without first converting to a number

  that you cannot actually re-use a variable

  that you can always convert any string to lower case

   that you can't actually change any characters in a string.

 5. The indexOf method tells you if a string of text contains a particular character, ____________________________________ for example

  the variable in a code snippet

  the @ sign in an email address

  the method in a function

  the function in a script

 6. If you run the script above, you'll find that Javascript returns a value of ____
var email = "[email protected]";
var at_sign = email.indexOf('@');
document.write( at_sign );

  1

  4

  3

  2

 7. What does this code do?
if ( at_sign == -1 ) {

document.write( "no @ sign in email address" );

}

else {

document.write("email address contains @ sign");

}

  checks the at_sign variable for a value of -1 and takes action accordingly.

  checks to see if the at_sign variable contains any negative numbers and throws up an error if so

  None of these options are valid

  checks the at_sign variable for a value of 2 and takes action accordingly

 8. If you want the character returned rather than its position number then you can use the Javascript string method _______
if ( at_sign == -1 ) {

document.write( "no @ sign in email address" );

}
else {

var character = email.charAt( at_sign );
document.write( character );

}

  strAt

   charAt

  methAt

  posAt

 9. What does this code do?
var email = "[email protected]";
var ending = email.substr( 0, 4 );
document.write(ending);

  This code grabs 4 characters from the email address, starting at position 0. (Position 0 is the first character in a string.)

  It grabs between 0 and 4 (that is 3) characters from the email address

  It grabs 4 -1 (that is 3) characters from the email address

  It grabs no characters from the email address, checking to see if it is valid first

 10. This grabs the last 4 characters from the email address, which are ______
var email = "[email protected]";
var ending = email.substr( -4 );
document.write(ending);

   .com

  33

  atmeme

  meme

 11. In Javascript code previously we checked for an @ sign. But what if the user enters more than one @ sign, in an attempt to deceive us?
var at_sign = "@[email protected]@[email protected]";
var counter = 0;
var i;

for ( i=0; i < at_sign.length; i++ ) {

var char = at_sign.substr( i, 1 );

if (char == "@" ) {

counter++;

}

}

document.write("number of times @ appears is: " + counter);

  Use an If statement and indexOf

  we can use indexOf

  Use a loop and indexOf

  Use a loop, an IF statement, and substr - go through all the letters of the email address, counting how many @ signs there are

 12. You can split up strings of text with the Javascript split method. In between the round brackets of split you type an optional delimiter
var email = "[email protected]_1.com, [email protected]_2.com, [email protected]_3.com";
var email_array = email.split(',');

  FALSE

  TRUE

 13. Suppose we wanted our email addresses to be separated by hyphens rather than commas. We could use the:

  delimiter method

  join method

  split method

  array method

 14. If you wanted no spaces at all between your strings then you can miss out the delimiter and just have ___________________
var new_string = email_array.join('-');

  just a single quote mark

  no quote marks at all

  three quote marks one after the other

  two quote marks together.

 15. In the following example, if you don't include the second number then JavaScript will fail to execute and cause an error
var email = "[email protected]";
var ending = email.substr( 0, 4 );
document.write(ending);

  TRUE

  FALSE