In: Computer Science
/*******************************************************************************
*
* Welcome to Assignment 1! In this assignment, you're going to be
practicing lots
* of new JavaScript programming techniques. You'll see comments
like this one:
*
* @param {String} name - the name to greet in the message
*
* These are specially formatted comments that define parameters to
functions,
* and tell use the type {String} or {Number}, and also the
parameter's name.
* Finally, we also explain a bit about how the parameter is used,
and what
* data it will have, whether it's optional, etc.
* Please complete this js file by writing the code in the specified
areas and then submit the assignment on blackboard
*******************************************************************************
* Problem 1: remove all spaces and dashes from a String value, and
make it lowercase.
*
* We want to be able to "crush" a string so that it contains no
spaces or dashes,
* and all letters are lower case. The crush() function should work
like this:
*
* crush('ABC') --> returns 'abc' (all lowercase)
* crush('abc') --> returns 'abc' (the string was already
correct, same value)
* crush('A BC') --> returns 'abc' (lowercase, space
removed)
* crush('a b- c') --> returns 'abc' (lowercase, spaces removed,
dash removed)
*
* @param {String} value - a string to be crushed
******************************************************************************/
function crush(value) {
// Replace this comment with your code...
}
/*******************************************************************************
* Problem 3: extract the Forward Sortation Area (FSA) from a Postal
Code
*
* The first three letters of a Canadian Postal Code are known as a
forward
* sortation area, and designate a geographic unit. See details
at:
*
* https://www.ic.gc.ca/eic/site/bsf-osb.nsf/eng/br03396.html
and
*
https://en.wikipedia.org/wiki/Postal_codes_in_Canada#Components_of_a_postal_code
*
* Given a postal code, you are asked to extract the FSA. If the
resulting FSA
* is invalid, you should throw an error message, otherwise return
the extracted FSA.
*
* For example:
*
* extractFSA('M2J 2X5') --> returns 'M2J' (valid FSA for Newnham
Campus)
* extractFSA('2MJ X25') --> throws an Error with the message,
'invalid FSA'
*
*
*
* A valid FSA is defined as:
*
* - first character is a letter, one of A, B, C, E, G, H, J, K, L,
M, N, P, R, S, T, V, X, Y (not D, F, I, O, Q, U, W)
* - second character is a digit, one of 0, 1, 2, 3, 4, 5, 6, 7, 8,
9
* - third character is any letter (A to Z)
*
* @param {String} postalCode - a postal code
******************************************************************************/
function extractFSA(postalCode) {
// Replace this comment with your code...
}
/*******************************************************************************
* Problem 4: convert an Age (number) to an Age Range Category
(string)
*
* For statistical reasons, we are interested in taking literal ages
like 21, 23, 26, etc
* and converting them to the string '20 to 29 years' so that we can
do grouping.
*
* The ageToRange() function takes an age as a Number and returns
the appropriate
* age range category string:
*
* ageToRange(21) --> returns '20 to 29 years'
* ageToRange(29) --> returns '20 to 29 years'
* ageToRange(30) --> returns '30 to 39 years'
*
* The age range categories you need to support are as
follows:
*
* '19 and younger',
* '20 to 29 Years',
* '30 to 39 Years',
* '40 to 49 Years',
* '50 to 59 Years',
* '60 to 69 Years',
* '70 to 79 Years',
* '80 to 89 Years',
* '90 and older'
*
* Your ageToRange() function should accept age Numbers from 0 to
125. If the
* age passed to your function is outside 0 to 125, throw an Error
with the message
* 'invalid age'.
*
* @param {Number} age - the age of the person
******************************************************************************/
function ageToRange(age) {
// Replace this comment with your code...
}
// Problem 1.
function crush(value){
var result="";
for (var i=0;i<value.length;i++)
{
if(value.charAt(i)!=' '&&value.charAt(i)!='-')
{
result=result+value.charAt(i);
}
}
result=result.toLowerCase();
return result;
}
// Problem 3
function extractFSA(postalCode)
{
var n=postalCode.length;
try{
if(n<3)
throw "Invalid FSA";
var str="ABCEGHIKLMNPRSTVXY";
if(str.indexOf(postalCode.charAt(0))<0)
throw "Invalid FSA";
if(!(postalCode.charAt(1)>='0'&&postalCode.charAt(1)<='9'))
{
throw "Invalid FSA";
}
if(!(postalCode.charAt(2)>='A'&&postalCode.charAt(2)<='Z'))
throw "Invalid FSA";
return postalCode.substr(0,3);
}
catch(err){
console.log(err);
}
return "";
}
// Problem 4
function ageToRange(age){
try{
if(age<0||age>125)
throw "Invalid age";
if(age<=19)
return "19 and younger";
if(age<=29)
return "20 to 29 Years";
if(age<=39)
return "30 to 39 Years";
if(age<=49)
return "40 to 49 Years";
if(age<=59)
return "50 to 59 Years";
if(age<=69)
return "60 to 69 Years";
if(age<=79)
return "70 to 79 Years";
if(age<=89)
return "80 to 89 Years";
if(age>=90)
return "90 and older";
}
catch(err){
console.log(err);
}
}