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

  variables

  inbuilt methods

  external generators

  internal variables

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

  toCase

  toLowerCase

  CaseLower

  Lower1

 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>

  method you wish to convert

  variable you want to convert

  variable you wish to delete

  function you wish to call

 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't actually change any characters in a string.

  that you can always convert any string to lower case

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

  the method in a function

  the function in a script

  the variable in a code snippet

  the @ sign in an email address

 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 );

  4

  1

  2

  3

 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 to see if the at_sign variable contains any negative numbers and throws up an error if so

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

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

  None of these options are valid

 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 );

}

   charAt

  strAt

  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 no characters from the email address, checking to see if it is valid first

  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

 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);

  meme

  33

   .com

  atmeme

 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 = "@meme@[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

  Use a loop and indexOf

  we can use 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 = "me@email_1.com, him@email_2.com, her@email_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:

  split method

  delimiter method

  array method

  join 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('-');

  two quote marks together.

  no quote marks at all

  just a single quote mark

  three quote marks one after the other

 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);

  FALSE

  TRUE