JavaScript Functions Practice

You can download the file fun1.js . It contains shell code for ten functions and describes what the functions are to do. Two solved examples are shown. This file will also be in ~cs/data so you can copy it diretctly to your server account by executing this command.

$ cp ~cs/data/fun1.js .

Do not forget the dot; it represents your cwd. You can then open the file in vi. Run this file using node. You can also copy functions into the console and test them there.

It is smart to test things and to try and break your own code.

Due Date and Turnin Upload your .js file to Canvas. Give it the name yourUserNameF1.js. This is due Mon 9 October at 5PM.

You can view its contents here.

/*
*  floating point numbers are not stored exactly. Use this to check
*  and see if two floating points numbers are close. Use
*  closeEnough(a,b) instead of a==b.  Returns True if a and b
*  are close enough.
*/
function closeEnough(x,y)
{
    return Math.abs(x - y) < 1e-10;
}
/*
* precondition:  x is a number
* postcondition:  returns the cube root of x
*/
function cubeRoot(x)
{
    return Math.pow(x, 1/3);
}
/*
*  prec:  name is a string
*  postc:  Puts the greeting Hello, name! to the console
*/
function greet(name)
{
    console.log("Hello, " + name + "!");
}
/*
* preconditon:  a string of the form firstName lastName is passed in for x
* postcondition:  returns a string containing initials in caps
* example inits("Peter Rabbit") -> "PR"
*/
function inits(x)
{
}
/*
* prec:  s is a string, ch is a one-character string
* returns true if ch is present in the string s, false otherwise.
*/
function hasChar(s, ch)
{
}
/*
* prec x is a number
* returns the sine of x using degrees instead of radians as angular measure
*/
function sinDeg(x)
{
}
/*
* prec: x is a single word
* postc:  returns the word translated into Pig Latin.  
* Here are the rules:  https://en.wikipedia.org/wiki/Pig_Latin
* There is some annoyingness here.  produce the best version you an.
*/
function swinify(w)
{
}
/*
* prec: x number representing degrees C
* postc: returns the equivalent temperatur in farenheit.
*/
function c2f(x)
{
}
/*
* preconditon: feetDown >= 0 is a number representing how deep you are in water (feet)
* postc:  returns the number of atmospheres of pressure present.
* Hint:  Look at the weight of the water above one square inch or cm
* above you.  you may assume you are at sea level, so that the air pressure is 1 atmosphere.
*/
function pressure(feetDown)
{
}
/*
* prec: x is a string with >= 3 characters
* postc: returns a string with the last three characers of x is reverse order.
*/
function artificial(x)
{
}
/*
* prec:  c is a color hex string with 6 digits.
* postc:  returns c with its red value zeroed out
* example visene("#ABCDEF") -> "#00CDEF"
*/
function visene(c)
{
}
/*
* prec: c is a color hex string with 6 digits
* postc:  returns c with its green value maxed.
* example: martian("#123456") -> "#12FF56"
*/
function martian(c)
{
}
/*
* prec:  s is a string
* postc: retuens s in lower case, with the word "cow" replacing every instance of the
* letter 'c' in s.
*/
function seeMooing(s)
{
}

/**************** Test  your functions here **********************/

console.log("cube root test x = 8: "  + closeEnough(cubeRoot(8), 2));
console.log("cube root test x = 0: "  + closeEnough(cubeRoot(0),0));
greet("Mephistopholes");