Client side programming

Things you can do with Strings

method/property description
name.length The number of characters in the string. (12)
name.charAt(0) Extracts the character at a given index/position ("J")
name.indexOf(" ") Searches the string for a character or string. Returns it's index (5)
name.slice(6) Returns a slice or substring of a string ("Rocker")
name.split(" ") Splits a string into an array, using a delimiter ( ["Jolly, "Rocker"] )
name.toLowerCase() Returns a copy of the string in lower case ("jolly rocker")
name.toUpperCase() Returns a copy of the string in upper case ("JOLLY ROCKER")

assuming you have:

var name = "Jolly Rocker"

slice(start[, end])

var name = "Alexander"
var part = name.slice(4,7) // and
 Alexander 
​012345678

slice(start[, end])

var name = "Alexander"
var part = name.slice(3) // xander
 Alexander 
​012345678

if the end-parameter is missing, it slices to the end of the string

Let's code a christmas tree

Create a random integer

between any two numbers

var min  = -15;  // ANY number you want
var max  = 10; // a higher number than min.

var random = Math.floor(Math.random() * (max-min+1)+min);

Random 1 or 0

var random = Math.round( Math.random() );

Let's code heads or tails

string.charAt()

the character at position...

var vowels = "aeiouy"
//            012345

var letterU = vowels.charAt(4)

Let's code

a random name generator

JSX1

By Johan Kohlin

JSX1

Conditionals and Loops exercise

  • 672