These are chat archives for FreeCodeCamp/HelpJavaScript

11th
Aug 2018
Ookma-Kyi
@ookma-kyi
Aug 11 2018 02:14
hi everyone
please help, i'm totally stumped
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 07:28

Hey everyone, I'm doing the Search and Replace algorithm and it seems that my .toUpperCase() methods are being read as a property. Does anyone know how I can fix this?

function myReplace(str, before, after) {
  var index = str.search(before);
  if (before[index] === before[index].toUpperCase) {
    after = after[0].toUpperCase()
    var replaced = str.replace(before, after);
  }
  else {
    var replaced = str.replace(before, after);
  }
  return replaced;
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

This is what my code is returning:

TypeError: Cannot read property 'toUpperCase' of undefined
Aditya
@ezioda004
Aug 11 2018 07:44
@ArchivalBoat50 You do have before[index].toUpperCase which is why its being interpreted as property and not method.
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 08:06

I fixed things a little and gained a few checkmarks but now I'm stuck with this:

function myReplace(str, before, after) {
  var index = str.search(before);
  var firstLetterCaps = str.charAt(index).toUpperCase() + str.slice(index + 1,before.length + index + 1); // Capitalized First Letter of before 
  if (before[index] === firstLetterCaps) { //If first letter of before is caps
    after = after.charAt(0).toUpperCase() + after.slice(1);
    var replaced = str.replace(before, after);
  }
  else {
    var replaced = str.replace(before, after);
  }
  return replaced;
}

myReplace("He is Sleeping on the couch", "Sleeping", "sitting")

My code should be returning this:
"He is Sitting on the couch"

But it's returning this instead:
"He is sitting on the couch"

Does anyone know why

Aditya
@ezioda004
Aug 11 2018 08:13
@ArchivalBoat50 if(before[index] === firstLetterCaps) this condition is problematic. Because index is from str, therefore wont be same for before
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 08:14
@ezioda004 Oh
@ezioda004 I replaced before[index] with str.charAt(index)
but it's still giving me the same thing
before.charAt(0) doesn't work either
Aditya
@ezioda004
Aug 11 2018 08:19
@ArchivalBoat50 firstLetterCaps returns a string. So you're comparing a char to a string.
str.charAt(0) === firstLetterCaps.charAt(0) should work
Btw you can also use strings as arrays, so simply use bracket notation to access characters, like str[0]
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 08:22
@ezioda004 Why does before === firstLetterCaps not work? I'm just comparing the whole word
Aditya
@ezioda004
Aug 11 2018 08:26
@ArchivalBoat50 Theres a trailing whitespace in firstLetterCaps because of before.length + index + 1); in str.slice(index + 1,before.length + index + 1);
If you make it before.length + index, then it should work.
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 08:28
@ezioda004 And thanks I got the whole thing to work using before.charAt(0) === firstLetterCaps.charAt(0);
Aditya
@ezioda004
Aug 11 2018 08:30
:sparkles: , try to console.log() these variables when something doesnt work :smile:
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 08:33
@ezioda004 What did you mean by trailing whitespace though? How did it happen?
Aditya
@ezioda004
Aug 11 2018 08:37
@ArchivalBoat50 You're also grabbing the whitespace after the word from str
Try console.log(before.length, firstLetterCaps.length), you'll see firstLetterCaps has 1 more length than before
So its like Sleeping + " " and not Sleeping
Seems like markdown trims the whitespace :laughing:
ArchivalBoat50
@ArchivalBoat50
Aug 11 2018 08:40
@ezioda004 Wow something like that seems hard to find. I found out I didn't need the + 1 at before.length + index + 1
chenqian
@M-cq
Aug 11 2018 16:24

var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];

function lookUp(firstName, prop){

if(contacts.hasOwnProperty(firstName)){
if(contacts.firstName!=prop){
return "No such contact";
}else{
return contacts.prop;
}
}else{
return "No such property";
}

}

lookUp("Akira", "likes");

i had try again and again , but nothing be found out ,
can someone help me ,thanks so much
Christopher McCormack
@cmccormack
Aug 11 2018 16:26
@M-cq wrap your code with three backticks on their own line so it's formatted properly on gitter:
```
Code
```
You can edit previous posts
@M-cq why if(contacts.firstName!=prop){?
chenqian
@M-cq
Aug 11 2018 16:28
thank you ,i will try to follow what you teach me
Ozarion
@Ozarion
Aug 11 2018 16:30
Hey. :)
Stuck here. says "Data array length is not what expected" in test cases.
// Find people who like "burrito". Sort them alphabetically by name,
// Limit the results to two documents, and hide their age.
// Chain `.find()`, `.sort()`, `.limit()`, `.select()`, and then `.exec()`,
// passing the `done(err, data)` callback to it.

var queryChain = function(done) {
  var foodToSearch = "burrito";

  Person.
    where({'favoriteFoods' : [foodToSearch]}).
    find().
    sort('name'). 
    select('-age').  
    limit(2).
    exec((err, docs) => {
      if (err) return done(err);
      return done(null, docs);
    });
};
chenqian
@M-cq
Aug 11 2018 16:31
I just wanted to compare whether they were equal
@cmccormack
Prakash Kumar
@ThePrakashKumar
Aug 11 2018 16:32
Any Basic Data Structure YouTube playlist?
Christopher McCormack
@cmccormack
Aug 11 2018 16:36
@M-cq prop is not the same as first name
@Ozarion this is fcc?
Ozarion
@Ozarion
Aug 11 2018 16:36
@cmccormack Yes
The last challenge on mongodb
Christopher McCormack
@cmccormack
Aug 11 2018 16:36
neat didn't know they had mongo exercises now
Ozarion
@Ozarion
Aug 11 2018 16:37
Yeah. There's a whole section. :)
Christopher McCormack
@cmccormack
Aug 11 2018 16:37
why an array for your where?
Ozarion
@Ozarion
Aug 11 2018 16:39
favoriteFoods is a propterty array which contains String type Foods. I want to search where favoriteFoods contains burrito. And do rest of filtering and sorting. Dont know what am I doing wrong :/
chenqian
@M-cq
Aug 11 2018 16:40
thanks @cmccormack
Ozarion
@Ozarion
Aug 11 2018 16:41
There's also fcc backend help. Should I ask it there?
Christopher McCormack
@cmccormack
Aug 11 2018 16:45
@Ozarion did you validate you're getting any docs?
Ozarion
@Ozarion
Aug 11 2018 16:47
Nope. :/ Let me see If I'm even receiving any. ..
Christopher McCormack
@cmccormack
Aug 11 2018 16:48
I don't think you need the braces around the query
and shouldn't find be above the criteria?
Moisés Man
@moigithub
Aug 11 2018 16:49
check the documentation.. for array operators
like $in (or this was from mongoose,, i dont recall )
Ozarion
@Ozarion
Aug 11 2018 16:49
Yes. Just checked Im not recieving any doc
Ozarion
@Ozarion
Aug 11 2018 16:51
@moigithub I'm using mongoose actually
shouldn't matter
Moisés Man
@moigithub
Aug 11 2018 16:54
according to the example...
IF favoriteFoods is an array .. doing
Person. find( { favoriteFoods : foodToSearch})
should work...
Ozarion
@Ozarion
Aug 11 2018 16:54

Thanks.

var queryChain = function(done) {
  var foodToSearch = "burrito";

  Person.
    find({'favoriteFoods' : {$in : [foodToSearch]}}).
    sort('name').
    select('-age').
    limit(2).
    exec((err, docs) => {
      if (err) return done(err);
      console.log(docs);
      return done(null, docs);
    });
};

worked.

btw is this the mongoose way or mongodb way of querying?
Christopher McCormack
@cmccormack
Aug 11 2018 17:00
I believe mongoose is just a bunch of helper functions but it still has to use the mongo driver to query the data
Ozarion
@Ozarion
Aug 11 2018 17:02
Got it.
chenqian
@M-cq
Aug 11 2018 17:28

//初始化变量
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];

function lookUp(firstName, prop){
// 请把你的代码写在这条注释以下
for(var i = 0 ; i < contacts.length; i++){

if(contacts[i].firstName == firstName){

 for(var j =0; j< contacts[i].likes.length; j++){

   if(contacts[i].likes[j] == prop){
     return contacts[i].likes[j];
   }else{ return "No such property";}
          }

}else{
return "No such contact";
}
}
}
lookUp("Akira", "likes");

still something be wrong ,
Christopher McCormack
@cmccormack
Aug 11 2018 17:31
@M-cq please follow the formatting request above :point_up: August 11, 2018 9:26 AM
without formatting it's very difficult to read
function lookUp(firstName, prop) {
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == firstName) {
      for (var j = 0; j < contacts[i].likes.length; j++) {
        if (contacts[i].likes[j] == prop) {
          return contacts[i].likes[j];
        } else {
          return "No such property";
        }
      }
    } else {
      return "No such contact";
    }
  }
}
lookUp("Akira", "likes");
chenqian
@M-cq
Aug 11 2018 17:32

var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];

function lookUp(firstName, prop){
for(var i = 0 ; i < contacts.length; i++){
if(contacts[i].firstName == firstName){
for(var j =0; j< contacts[i].likes.length; j++){
if(contacts[i].likes[j] == prop){
return contacts[i].likes[j]; }else{
return "No such property"; }}
}else{ return "No such contact";}}}
lookUp("Akira", "likes");

how do you do that, so good
Christopher McCormack
@cmccormack
Aug 11 2018 17:32
@M-cq read what I wrote...
chenqian
@M-cq
Aug 11 2018 17:33
ok ,thanks
Christopher McCormack
@cmccormack
Aug 11 2018 17:33
image.png
@M-cq you are returning No such contact after only checking the first contact if they do not match
@M-cq you should only return that after your first for loop has completed
@M-cq similar with No such property in your inner loop
@M-cq also you may want to use contacts[i].hasOwnProperty method for checking if an item is in an array, it's a bit cleaner
chenqian
@M-cq
Aug 11 2018 17:37
thank you for your help , i will try it again @cmccormack
Christopher McCormack
@cmccormack
Aug 11 2018 17:39
@M-cq :thumbsup: