Docsity

String Manipulations - Introduction to Computing - Lecture Slides, Slides of Introduction to Computing

Partial preview of the text

Download String Manipulations - Introduction to Computing - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

CS101 Introduction to Computing

Lecture 38

String Manipulations

(Web Development Lecture 13)

During the last lecture we discussed Mathematical Methods

  • We looked at the properties and methods of JavaScript’s Math object
  • We produced solutions for simple problems using several methods of the Math object

Mathematical Functions in JavaScript

  • In addition to the simple arithmetic operations (e.g. +, *, etc.) JavaScript supports several advanced mathematical operations as well
  • Notationaly, these functions are accessed by referring to various methods of the Math object
  • Moreover, this object also contains several useful mathematical constants as its properties
  • This object has no use, but of a placeholder (^) Docsity.com 4

Properties

Math.PI

Math.E

Math.LN

Math.LN

Math.LOG2E

Math.LOG10E

Math.SQRT

Math.SQRT1_ 5

Note the CAPITAL lettering of all properties

sin( r ), cos( r ), tan( r )

Standard trigonometric functions

Returns the sine, cosine or tangent of ‘r’, where ‘r’ is specified in radians

EXAMPLE

document.write( Math.cos( Math.PI / 4 ) )

7

asin( x ), acos( x ), atan( x )

Standard inverse-trigonometric functions

Returns the arcsine, arccosine or arctangent of ‘r’

in radians

EXAMPLE

document.write( Math.asin( 1 ) )

8

10

Returns Math.E raised to the power x

exp( x )

Returns the the natural logarithm of x

log( x )

Math.E → 1

ceil( x )

11

Returns integer nearest to x

Returns largest integer that is less than or equal to x

Returns smallest integer that is greater than or equal to x

round( x ) floor( x )

13

Returns the smaller of x and y

min( x, y )

Returns the larger of x and y

max( x, y )

random( )

14

Returns a randomly-selected, floating-point number between 0 and 1

EXAMPLE

document.write( Math.random( ) )

Today’s Goal (String Manipulation)

  • To become familiar with methods used for manipulating strings
  • To become able to solve simple problems involving strings

String Manipulation Examples

  • Combine these words into a sentence i.e. take these strings and concatenate them into one
  • Break this string into smaller ones
  • Convert this string into upper case
  • See if a particular character exists in a string
  • Find the length of a string
  • Convert this string into a number (^) Docsity.com^17

Example

name = “BHOLA” ;

document.write( “The length of the string ‘name’ is ”, name.length ) ;

19

The length of the string ‘name’ is 5

Let us now revisit an example that we first

discussed in the 18th^ lecture

Let us see how we put the ‘length’ property

of a string to good use