Guest User

Untitled

a guest
Jun 23rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. "use strict";
  2.  
  3. var express = require('express');
  4. let jobs = require('./jobs'); // load the jobs module
  5.  
  6. var router = module.exports = express.Router();
  7.  
  8. let jobList = jobs.readJobs(); // read the jobs.csv file
  9.  
  10. // Just a test to bring up the page
  11. router.get('/', function (req, res) {
  12. res.render('final', {jobs: jobList, average: 0, title: "All Jobs"});
  13. });
  14.  
  15.  
  16. // just a check to see if the code works
  17. router.get('/check/:check', function(req, res) {
  18. res.send(req.params.check);
  19. });
  20.  
  21.  
  22. // Here is your endpoint needed to service the request from the POST request from the little mini form
  23. router.post('/jobSearch', function (req, res) {
  24. // TODO 1 - Create variables needed by your code. See TO DONT below, it mentions the variables needed
  25.  
  26.  
  27.  
  28. // TODO 2 - List of things to do
  29. // TODO A - IF the category and city fields are empty THEN list ALL jobs
  30. // TODO B - IF the category and city fields BOTH have data then THEN list all jobs in that city for that category
  31. // TODO C - IF the category field has data THEN list all jobs in that category
  32. // TODO D - IF the city field has data THEN list all jobs in that city
  33.  
  34. // TODO actually TO DONT
  35. // These lines of code will NOT be changed. Below are listed the variables you need to assign
  36. // outList - use to contain the list of jobs that fit the users criteria from the web page
  37. // average - use to contain the average salary for the jobs you find
  38. // title - use to create a descriptive title for the page - "Average Salary for Managers in Dallas"
  39. average = average.formatMoney(0, "$ ");
  40. res.render('final', {jobs: outList, average: average, title: title});
  41. });
  42.  
  43. // TODO 3 - Add a router.get to respond to request from the browser
  44. // The router path will be /job/:jobId
  45. // Your code will take the req.params.jobId field
  46. // and use that to select a job from the jobList array
  47. // your code will use res.send to return the job
  48.  
  49. // your code here
  50.  
  51.  
  52. /*
  53. * Reusing our function from the JavaScript part of the test
  54. * Given a list of jobs and a category, build an array with jobs matching that category
  55. */
  56. function getCategoryJobs(jobs, category) {
  57. let list = []; // empty array to hold jobs that match
  58. for (let job of jobs) { // look at all jobs in array
  59. if (job.category === category) { // does this job match the category we are interested in?
  60. list.push(job); // yup, save it
  61. }
  62. }
  63. return list; // send back the jobs found
  64. }
  65.  
  66.  
  67. /*
  68. * Reusing our function from the JavaScript part of the test
  69. * Given a list of jobs and a city, build an array with jobs matching that city
  70. */
  71. function getCityJobs(jobs, city) {
  72. let list = []; // empty array to hold jobs that match
  73. for (let job of jobs) { // look at all jobs in array
  74. if (job.city === city) { // does this job match the city we are interested in?
  75. list.push(job); // yup, save it
  76. }
  77. }
  78. return list; // send back the jobs found
  79. }
  80.  
  81.  
  82. /*
  83. * Reusing our function from the JavaScript part of the test
  84. * Calculate the average salary for a given list of jobs
  85. */
  86. function getAverageSalary(jobs) {
  87. let totalSalary = 0;
  88. for (let job of jobs) { // look at all jobs in array
  89. totalSalary += job.salary;
  90. }
  91. return totalSalary / jobs.length;
  92. }
  93.  
  94.  
  95. // Extend the default Number object with a formatMoney() method:
  96. // usage: someVar.formatMoney(decimalPlaces, symbol, thousandsSeparator, decimalSeparator)
  97. // defaults: (2, "$", ",", ".")
  98. Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
  99. places = !isNaN(places = Math.abs(places)) ? places : 2;
  100. symbol = symbol !== undefined ? symbol : "$";
  101. thousand = thousand || ",";
  102. decimal = decimal || ".";
  103. var number = this,
  104. negative = number < 0 ? "-" : "",
  105. i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
  106. j = (j = i.length) > 3 ? j % 3 : 0;
  107. return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
  108. };
Add Comment
Please, Sign In to add comment