11 November 2021

Function Writing


/* 
 *  precondtions: s is a string, a is one-character string.
 *  postconditions:
 *  returns s if a is not present in s and returns s truncted 
 *  before a if a is present in s.
 /*
function chopper(s, a)
{
    if( s.includes(a))
    {
        //where is a?
        let n = s.indexOf(a);
        return s.substring(0,n);
    }
}

Wheel Invention Question Is there a smart way to sort a numerical array?



/* 
 *  precondtions: x is a non-empty numerical array
 *  postconditions:   returns the median of x
 /*
function median(x)
{
    x.sort()
    let n = x.length;
    if(n % 2 == 0)   //EVEN CASE
    {
        return (x[n/2] + x[n/2 - 1])/2;
    }
    else             //ODD cASE
    {
        return x[(n-1)/2];
    }
}

The next two functions look similar but have an important difference.


/* 
 *  precondtions: x is an array
 *  postconditions:  This function has the side-effect of
 *  rotating the array right by clipping off the last element
 *  an making it the first.  It has no return value
 *  If x is empty,  do nothing.
 /*
function rotateRight(x)
{
    if(x ==[])
    {
        return 
    }
    let end = x.pop();
    x.unshift(end);
}

Use array methods for the next two problems.


/* 
 *  precondtions: x is an array
 *  postconditions:
 *  returns a new array that has been rotated right.
 *  If x is empty, just return an empty array.
 /*
function rightRotation(x)
{
    if(x ==[])
    {
        return [];
    }
    let allButLast = x.slice(0, x.length) - 1);   //new array
    allButLast.unshift(x[x.length = 1);
    return allButLast;
}

/* 
 *  precondtions: x is a number
 *  postconditions:
 *  returns the sine of x, where x is unitted in degrees
 /*
function sinD()
{
}

/* 
 *  precondtions: node is a node from a web page,
 *  color is a color
 *  postconditions: changes the background color of the 
 *  specified node to the specified color.
 */
function changeBackground(node, color)
{
}

/* 
 *  precondtions: s is a string, a is one-chaacter string.
 *  postconditions:
 *  returns s if a is not present in s and returns s truncted 
 *  before a if a is present in s.
function chopper(s, a)
{
    if( s.includes(a))
    {
        return s;        
    }
}

/* 
 *  precondtions: s is a string, a is one-chaacter string.
 *  postconditions:
 *  returns s if a is not present in s and returns s truncted 
 *  before a if a is present in s.
function chopper(s, a)
{
    if( s.includes(a))
    {
        return s;        
    }
}

/* 
 *  precondtions: s is a string, a is one-chaacter string.
 *  postconditions:
 *  returns s if a is not present in s and returns s truncted 
 *  before a if a is present in s.
function chopper(s, a)
{
    if( s.includes(a))
    {
        return s;        
    }
}

/* 
 *  precondtions: s is a string, a is one-chaacter string.
 *  postconditions:
 *  returns s if a is not present in s and returns s truncted 
 *  before a if a is present in s.
function chopper(s, a)
{
    if( s.includes(a))
    {
        return s;        
    }
}

/* 
 *  precondtions: s is a string, a is one-chaacter string.
 *  postconditions:
 *  returns s if a is not present in s and returns s truncted 
 *  before a if a is present in s.
function chopper(s, a)
{
    if( s.includes(a))
    {
        return s;        
    }
}