Question

In: Computer Science

/******************************************************************************* * * Welcome to Assignment 1! In this assignment, you're going to be practicing lots...

/*******************************************************************************
*
* 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...
}

Solutions

Expert Solution

// 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);
  }
}

Related Solutions

In this assignment you are going to use the menu you created in Assignment 1 to...
In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
PLEASE SHOW WORK FOR ALL, IF YOU'RE ONLY GOING TO DO ONE OF THESE AND NOT...
PLEASE SHOW WORK FOR ALL, IF YOU'RE ONLY GOING TO DO ONE OF THESE AND NOT ALL, DONT ANSWER 12. P. Rose, Inc. is considering a new four-year expansion project that requires an initial fixed asset investment of $1,950,000. The asset will be depreciated straight-line to zero over its six-year tax life, after which time it will be worthless. The project is estimated to generate $2,145,000 in annual sales, with costs of $1,205,000. The fixed asset will have a market...
PLEASE SHOW WORK FOR ALL, IF YOU'RE ONLY GOING TO DO ONE OF THESE AND NOT...
PLEASE SHOW WORK FOR ALL, IF YOU'RE ONLY GOING TO DO ONE OF THESE AND NOT ALL, DONT ANSWER 1. Assume that the risk-free rate is 5 percent and that the market risk premium is 7 percent. If a stock has a required rate of return of 13.75 percent, what must its beta be? a. 1.25 b. 1.35 c. 1.37 d. 1.60 e. 1.96 2. Your family recently obtained a 30-year $100,000 fixed-rate mortgage. Which of the following statements is...
This week you're going to be discussing the goals, values and principles of psychiatric rehabilitation. In...
This week you're going to be discussing the goals, values and principles of psychiatric rehabilitation. In what ways do they support people’s personal recovery?  Use one or two of the elements when 'stating your case'!
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
For this 1-2 page assignment, you are going to watch the video of a speech and...
For this 1-2 page assignment, you are going to watch the video of a speech and write a peer review as if the speaker would get the feedback from you in written form. In the review please address the following aspects of the speech, giving clear examples from the speech and then critique the student’s performance. At least three aspects of delivery At least two aspects of content Was the speech effective? Student Speeches for Analysis
The importance of practicing FIFO method and LIFO method in inventory evaluation for businesses. The assignment...
The importance of practicing FIFO method and LIFO method in inventory evaluation for businesses. The assignment needs to present advantages that the businesses will gain from applying FIFO and LIFO methods. In addition, the   assignment should convey logical reasoning for the information presented and clarity in conveying the required points
Important: If you're going to write make sure your writing is neat, legible, and easy to...
Important: If you're going to write make sure your writing is neat, legible, and easy to read. Please write in print. Please do not write in cursive. Thank you Important:I have the answers to the questions. I'll provide them bellow the questions. However, I don't understand why it's the correct answer. Bellow the answers I'll ask the questions I have about the answer given. Thank you QUESTION: How will a decrease in output during a recession affect (explain) a)business fixed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT