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 ________________

  Lower1

  toLowerCase

  CaseLower

  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>

  method you wish to convert

  function you wish to call

  variable you want to convert

  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 re-use a variable

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

  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 function in a script

  the @ sign in an email address

  the method in a function

  the variable in a code snippet

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

  2

  4

  1

  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 2 and takes action accordingly

  None of these options are valid

  checks the at_sign variable for a value of -1 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 );

}

  posAt

   charAt

  strAt

  methAt

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

  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

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

 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

  atmeme

   .com

 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 a loop, an IF statement, and substr - go through all the letters of the email address, counting how many @ signs there are

  Use a loop and indexOf

  we can use indexOf

  Use an If statement and indexOf

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

  TRUE

  FALSE

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

  delimiter method

  join method

  array method

  split 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.

  three quote marks one after the other

  no quote marks at all

  just a single quote mark

 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