These are chat archives for FreeCodeCamp/Help

30th
Jan 2015
Suzanne Atkinson
@AdventureBear
Jan 30 2015 02:41
Do you think anyone here could debug my express.js routes from the codeschool challenge part 4?
app.route('/cities/:name')
.get(function(request,response) {
    var cityName

    var description = cities[request.params.name];
    if (!description) {
        response.status(404).json('No description found for ' + request.params.name);
    }else {
        response.json(description);
    }  
})

.delete(function(request, response) {
  if (cities[request.cityName]) {
    delete cities[request.cityName];
    response.sendStatus(200);
  }else {
      response.sendStatus(404);    
  }
});
when I try to delete a city, I get a 404 Not found with a properly formed url like this:
DELETE http://localhost:3001/cities/Lotopia 404 (Not Found)
so it looks like the jquery event handler is picking up my click on the delete image and sending proper city data to the route. But it's not deleting
James McShane
@jmcshane
Jan 30 2015 03:07
@AdventureBear I'm taking a look at it for you
that var cityName isn't in scope for deletes
you should get the city name using var name = request.params.name
Suzanne Atkinson
@AdventureBear
Jan 30 2015 03:18
hmmm...
YEahh!!! THat worked! :)
Thank you!!
I knew that was messed up somehow but by the time I got through all that my midn was a bit fried.
Jus tfinished the last module of building blocks of express, and passed...now going to try and refactor my own code. Then I'll be DONE for the night!
thanks
Suzanne Atkinson
@AdventureBear
Jan 30 2015 04:48
code refactoring...big flop!
Dominic Jones
@towwiow
Jan 30 2015 04:51
lol, uh-oh.
Harry Karapetyan
@hk99
Jan 30 2015 04:53
has anyone tried the sumFibs challenge here?
Suzanne Atkinson
@AdventureBear
Jan 30 2015 04:56
nope
node app.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'routes/cities'
there is more to the errors.
Harry Karapetyan
@hk99
Jan 30 2015 04:57
are you exporting the route?
Suzanne Atkinson
@AdventureBear
Jan 30 2015 04:57
here's my app.js

```var express = require('express');
var app = express();
var router = require('routes/cities');

app.use('/cities', router);
app.listen(3001, function(){
console.log('Running Express');
});
```

var express = require('express');
var app = express();
var router = require('routes/cities');

app.use('/cities', router);
app.listen(3001, function(){
    console.log('Running Express');
});
oh, so in folder routes/cities.js
I have this at the very end
module.exports = router;
want to see the rest of it? here's the code at the beginning
var express = require('express');

var bodyParser = require('body-parser');
var parseUrlencoded = bodyParser.urlencoded({extended: false});
Harry Karapetyan
@hk99
Jan 30 2015 04:59
try var router = require('./routes/cities');
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:00
@hk99 that worked, to start the router, yay! But now I ahve a new error in teh browser
Cannot GET /
Harry Karapetyan
@hk99
Jan 30 2015 05:02
need to see your router
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:03
OK, here is the whole thing (./routes/cities.js)
This message was deleted
var express = require('express');

var bodyParser = require('body-parser');
var parseUrlencoded = bodyParser.urlencoded({extended: false});

// In memory store for the cities in our application
var cities = {
  'Lotopia': 'Rough and mountainous',
  'Caspiana': 'Sky-top island',
  'Indigo': 'Vibrant and thriving',
  'Paradise': 'Lush, green plantation',
  'Flotilla': 'Bustling urban oasis'
};


var router = express.Router();

router.route('/')
.get(function(request,response) {
    if(request.query.search) {
    response.json(citySearch(request.query.search));
  } else {
    response.json(Object.keys(cities));
  }
})
.post(parseUrlencoded, function(request,response) {
  if(request.body.description.length > 4) {
    var city = createCity(request.body.name, request.body.description);
    response.status(201).json(city);
  } else {
    response.status(400).json('Invalid City');
  }
});


router.route('/:name')
 .all(function(request, response, next) {
     request.cityName = parseCityName(request.params.name);
})
.get(function(request,response) {
    var description = cities[request.params.name];
    if (!description) {
        response.status(404).json('No description found for ' + request.params.name);
    }else {
        response.json(description);
    }  
})
.delete(function(request, response) {
var cityName = request.params.name;

  if (cities[cityName]) {
    delete cities[cityName];
    response.sendStatus(200);
  }else {
      response.sendStatus(404);    
  }
});

//Adds a new city to the memory store
var createCity = function(name, description){
  cities[name] = description;
  return name; 
}


// Uppercase the city name.
function parseCityName(name) {
  var parsedName = name[0].toUpperCase() + name.slice(1).toLowerCase();
  return parsedName;
}

// Searches for keyword in description and returns the city
function citySearch (keyword) {
  var regexp = RegExp(keyword, 'i');
  var result = cities.filter(function (city) {
    return city.match(regexp);
  });
  return result;
}

module.exports = router;
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:09
it worked until I refactored it
Harry Karapetyan
@hk99
Jan 30 2015 05:09
what did you change?
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:14
This stuff:
var router = express.Router();

router.route('/')
used to be
Harry Karapetyan
@hk99
Jan 30 2015 05:17
did you refactor the citySearch function?
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:17
```app.route('/cities')
app.route('/cities')
and in the app.js file, now it's in teh routes/cities.js file
no I didn't , le tme look at that
Harry Karapetyan
@hk99
Jan 30 2015 05:19
one thing is you cant use filter method on an object, only arrays
don't know how that worked if you didn't change it
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:19
Well I had some pieces from the tutorial that I was commenting out b/c the later stages didn't use all the same code.
let me go look at that specifically
Harry Karapetyan
@hk99
Jan 30 2015 05:20
the only thing else I can think of is if your visiting /cities in your browser
not just /
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:20
visiiting /cities gives me an array of city names
["Lotopia","Caspiana","Indigo","Paradise","Flotilla"]
visiting / gives me the 'cannot GET' error.
wait...
Harry Karapetyan
@hk99
Jan 30 2015 05:21
there you go thats your route
now you need a route for root /
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:22
app.get('/', function (request, response) {
  response.sendFile(__dirname + '/public/index.html');
});
I had this commented out in app.js
that pulls the index but the stylesheet is no logner attached, and it's not loading the city objects. :(
sigh.
I am guessing that with a generator all this is taken care of to a better degree, but I'd still like to understand it.
I"m going to bed, thanks for looking at the code @hk99
Cristián Berríos
@crisberrios
Jan 30 2015 05:24
check headers client side
Bin Ury
@teddy-error
Jan 30 2015 05:24
don't give up!
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:25
lol...i need sleep! client side headers! hmm
Harry Karapetyan
@hk99
Jan 30 2015 05:25
i wouldn't rely on generators, just start small and take it step by step
otherwise a break isn't a bad idea, goodnight!
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:26
OK, the last thing I"ll post
my client.js (which I forgot all about)
starts like this
$(function(){
    $.get('routes/cities',appendToList);
I just added the 'routes/cities', it was just 'cities' before. Also tried './routes/cities'. now I feel like I"m just guessing!
Goodnight all, I'll try again tomorrow.
Harry Karapetyan
@hk99
Jan 30 2015 05:28
do you know the difference between the two?
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:29
I would assume the path that is called, but now I am not really sure. Lots of info with this last express.js module
Harry Karapetyan
@hk99
Jan 30 2015 05:29
node looks for routes/cities inside the folder its in
./ in front tells node to go up one folder then look from there
Suzanne Atkinson
@AdventureBear
Jan 30 2015 05:29
This jquery/client.js wasn't explained well. Myabe I move client.js inside the routes folder? No, that's silly....
I thought ./ was the same folder, ../ (two dots) was one folder up
?
anyway thanks a bunch I'll take a look in the am. crashing now
Harry Karapetyan
@hk99
Jan 30 2015 05:30
ya i would check, anyway no prob night
Bin Ury
@teddy-error
Jan 30 2015 08:40
var Vector = function(x,y){
    this.x = x;
    this.y = y;

    get: function length(){
        return "dog";
    }
}
Vector.prototype.plus = function(v){
    return new Vector(this.x + v.x, this.y + v.y);
}

Vector.prototype.minus = function(v){
    return new Vector(this.x - v.x, this.y - v.y);
}
is this seriously how object construction typically goes in Javascript?
Suzanne Atkinson
@AdventureBear
Jan 30 2015 13:47
What wrong with it? ( I am new so just asking)
Nathan
@terakilobyte
Jan 30 2015 14:33
nothing is wrong with it
besides node development really isn't about oo anyway, it's functional
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 15:51
var me = {}
    me.object1 = 777;
    me.object2 = 16;
    me.object3 = 1645;
problem?
Justin Rogers
@yaskyj
Jan 30 2015 15:56
What's going wrong?
Also, there's no semicolon after the first statement.
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 16:00
@yaskyj
var me = {};
    me.object1 = 777;
    me.object2 = 16;
    me.object3 = 1645;
still wrong
Nathan
@terakilobyte
Jan 30 2015 16:00
in node that works fine
Justin Rogers
@yaskyj
Jan 30 2015 16:00
^ yes
Nathan
@terakilobyte
Jan 30 2015 16:00
what environment are you working in?
Justin Rogers
@yaskyj
Jan 30 2015 16:01
What is it doing that's wrong?
in the developer console
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 16:08
All solved now
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:30
var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Weiman",
        number: 03004444,
        address: ["blah","blah","blah"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Woz",
        number: 985856950,
        address: ["blah","blah","blah"]
    }
};
var list = function(list){
    for(list in friends){
        consloe.log(friends[list]);
    }
}
Code ^
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 17:30
"consloe"
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:30

Instructiions:

Create a function list that takes a single parameter.
In the body of the function, write a for/in loop.
In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out "bill" and "steve".)

@piecedigital New Error: It looks like your list function isn't printing the right output.
Ben Stoltz
@benstoltz
Jan 30 2015 17:31
(FYI, just in terms of best practices, using For-in is not best practice with iterating over array elements
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:31
@benstoltz codecademy challenge requirements
Ben Stoltz
@benstoltz
Jan 30 2015 17:32
I know :) just was tossing that in for folks who might not know
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:32
:smile:
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 17:32
try: console.log(friends[list].name);
I think they just want the name.
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:34
@piecedigital undefined
Cristián Berríos
@crisberrios
Jan 30 2015 17:37
var list = function(amigo) {
    for(var amigo in friends) {
        console.log(amigo);
    }
};
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 17:37
Function isn't supposed to take an arguement
var list = function(){
    for(var dude in friends){
        console.log(dude);
        }
    };
Cristián Berríos
@crisberrios
Jan 30 2015 17:37
^
or at least
pass the array as an argument
function(friends)
not function amigo
when you do for(var X in Y)
you are defining X
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:39
var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Weiman",
        number: 03004444,
        address: ["blah","blah","blah"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Woz",
        number: 985856950,
        address: ["blah","blah","blah"]
    }
};
var list = function (friends) {
for (var firstName in friends){
console.log(firstName);
}
};
Correct code ^
thanks to Kapil Dutta
Thanks for all your help guys
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 17:41
Just to note, the function doesn't need an argument. It's doing nothing in this instance.
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 17:44
@piecedigital oh
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 18:13
var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Weiman",
        number: 03004444,
        address: ["blah","blah","blah"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Woz",
        number: 985856950,
        address: ["blah","blah","blah"]
    }
};
var list = function (friends) {
    for (var firstName in friends){
        console.log(firstName);
    }
};
var search = function (friends) {
    for (var firstName in friends){
        console.log(firstName);
        console.log(lastName);
        console.log(number);
        console.log(address);
    }
};
Problem now?
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 18:14
Ok, think of it like this:
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 18:14
@piecedigital problem spotted
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 18:14
firstName, the variable in your loop, think of it as the errand boy. The loop in the function is sending it into the friends object and returning whatever is in there
or whatever you tell it to return.
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 18:17
@piecedigital How do i console.log() and return it?
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 18:19
console.log(friends[firstName].firstName);
read this
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 18:21
okay
Bin Ury
@teddy-error
Jan 30 2015 18:21
for keys with spaces or symbols you need to use square bracket notion to access properties of objects:
ob1 = new Object();
ob1["miles ran"]
ob1.height
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 18:24
var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Weiman",
        number: 03004444,
        address: ["blah","blah","blah"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Woz",
        number: 985856950,
        address: ["blah","blah","blah"]
    }
};
var list = function (friends) {
    for (var firstName in friends){
        console.log(firstName);
    }
};
var search = function (friends) {
    for (var firstName in friends){
        if(friends[firstName] === name){
            console.log(friend[firstName]);
        }
    }
};
problem?
I need to end this and then sleep
The hell I'm sleeping
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 18:26
the firstName variable in your loop has nothing to do with your friends list; it is only the variable of the loop. it goes through the friends object and can return bill and steve
Ammar Ali Shah
@AmmarAliShah
Jan 30 2015 18:26
I'm tackling this in the morning
AmmarAliShah @AmmarAliShah is going to sleep
Bin Ury
@teddy-error
Jan 30 2015 18:27
go to sleep, read the book when you wakeup. that's not how you iterate over objects
Ryan Malm
@Rybar
Jan 30 2015 18:27
trying to do for each, I assume
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 18:28
It's codecademy, it's asking for a for in loop.
@ama
Justin Rogers
@yaskyj
Jan 30 2015 18:28
It might be clearer if the for loops contained:
var friend in friends
Darryl Dixon - Piece Digital
@piecedigital
Jan 30 2015 18:29
@AmmarAliShah we can pair on this if you want and I can try to explain it better.
Ryan Malm
@Rybar
Jan 30 2015 18:31
right the loop is technically constructed correctly, but the name for the key is misleading, and the loop contents isn't calling the object keys right either.
Justin Rogers
@yaskyj
Jan 30 2015 18:37
Also, where is the "name" variable declared?
Bin Ury
@teddy-error
Jan 30 2015 18:38
he meant != undefined i think
Suzanne Atkinson
@AdventureBear
Jan 30 2015 19:47
hello!
Alex Dixon
@alex-dixon
Jan 30 2015 20:12
  function deleteEvens(array) {
    for (var i = array.length - 1; i >= 0; i -= 1) {
      if (array[i] % 2 === 0) {
        array[i].pop();
      }
    }
    return array;
  }
Help!
Justin Rogers
@yaskyj
Jan 30 2015 20:14
Pop takes the last item in the array off.
Alex Dixon
@alex-dixon
Jan 30 2015 20:14
Dur
Thanks
:)
Cristián Berríos
@crisberrios
Jan 30 2015 20:20
look into splice() method
Alex Dixon
@alex-dixon
Jan 30 2015 20:21
@crisberrios Thank you. Everything worked after that. Hopefully I will remember it this time :)
Cristián Berríos
@crisberrios
Jan 30 2015 20:21
or map() if you don't need to specifically use a for loop
Nathan
@terakilobyte
Jan 30 2015 20:33
be careful with map if you are modifying the size of the array
if you were to "delete" an element you will get back an array of the same size with an undefined in the deleted element's place
there be dragons
for something like deleteEvens, filter() is perfect
function deleteEvens(arr) {
  return arr.filter(elem) {
    return (!elem % 2);
  });
}
or something close to that
Cristián Berríos
@crisberrios
Jan 30 2015 20:49
@terakilobyte my bad, filter should be used in that situation
Nathan
@terakilobyte
Jan 30 2015 20:49
not a bad!
I slammed my head against map for a while
until I figured out the contract with map
it will return an array with the same amount of elements
Cristián Berríos
@crisberrios
Jan 30 2015 20:55
time to lunch, brb