Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- var express = require('express');
- let jobs = require('./jobs'); // load the jobs module
- var router = module.exports = express.Router();
- let jobList = jobs.readJobs(); // read the jobs.csv file
- // Just a test to bring up the page
- router.get('/', function (req, res) {
- res.render('final', {jobs: jobList, average: 0, title: "All Jobs"});
- });
- // just a check to see if the code works
- router.get('/check/:check', function(req, res) {
- res.send(req.params.check);
- });
- // Here is your endpoint needed to service the request from the POST request from the little mini form
- router.post('/jobSearch', function (req, res) {
- // TODO 1 - Create variables needed by your code. See TO DONT below, it mentions the variables needed
- // TODO 2 - List of things to do
- // TODO A - IF the category and city fields are empty THEN list ALL jobs
- // TODO B - IF the category and city fields BOTH have data then THEN list all jobs in that city for that category
- // TODO C - IF the category field has data THEN list all jobs in that category
- // TODO D - IF the city field has data THEN list all jobs in that city
- // TODO actually TO DONT
- // These lines of code will NOT be changed. Below are listed the variables you need to assign
- // outList - use to contain the list of jobs that fit the users criteria from the web page
- // average - use to contain the average salary for the jobs you find
- // title - use to create a descriptive title for the page - "Average Salary for Managers in Dallas"
- average = average.formatMoney(0, "$ ");
- res.render('final', {jobs: outList, average: average, title: title});
- });
- // TODO 3 - Add a router.get to respond to request from the browser
- // The router path will be /job/:jobId
- // Your code will take the req.params.jobId field
- // and use that to select a job from the jobList array
- // your code will use res.send to return the job
- // your code here
- /*
- * Reusing our function from the JavaScript part of the test
- * Given a list of jobs and a category, build an array with jobs matching that category
- */
- function getCategoryJobs(jobs, category) {
- let list = []; // empty array to hold jobs that match
- for (let job of jobs) { // look at all jobs in array
- if (job.category === category) { // does this job match the category we are interested in?
- list.push(job); // yup, save it
- }
- }
- return list; // send back the jobs found
- }
- /*
- * Reusing our function from the JavaScript part of the test
- * Given a list of jobs and a city, build an array with jobs matching that city
- */
- function getCityJobs(jobs, city) {
- let list = []; // empty array to hold jobs that match
- for (let job of jobs) { // look at all jobs in array
- if (job.city === city) { // does this job match the city we are interested in?
- list.push(job); // yup, save it
- }
- }
- return list; // send back the jobs found
- }
- /*
- * Reusing our function from the JavaScript part of the test
- * Calculate the average salary for a given list of jobs
- */
- function getAverageSalary(jobs) {
- let totalSalary = 0;
- for (let job of jobs) { // look at all jobs in array
- totalSalary += job.salary;
- }
- return totalSalary / jobs.length;
- }
- // Extend the default Number object with a formatMoney() method:
- // usage: someVar.formatMoney(decimalPlaces, symbol, thousandsSeparator, decimalSeparator)
- // defaults: (2, "$", ",", ".")
- Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
- places = !isNaN(places = Math.abs(places)) ? places : 2;
- symbol = symbol !== undefined ? symbol : "$";
- thousand = thousand || ",";
- decimal = decimal || ".";
- var number = this,
- negative = number < 0 ? "-" : "",
- i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
- j = (j = i.length) > 3 ? j % 3 : 0;
- 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) : "");
- };
Add Comment
Please, Sign In to add comment