These are chat archives for FreeCodeCamp/HelpJavaScript

7th
Jun 2018
Ayush Bahuguna
@relentless-coder
Jun 07 2018 06:22
hey, any react developers in here?
JJ Megma
@megma_jj_twitter
Jun 07 2018 06:59
@relentless-coder I have done some React
Ayush Bahuguna
@relentless-coder
Jun 07 2018 07:06
@megma_jj_twitter have you worked with re-ducks code architecture?
Claudio Restifo
@Marmiz
Jun 07 2018 07:23
you mean redux?
oh wait, you mean the ducks approach?
of having all actions creator inside a single file?
Ayush Bahuguna
@relentless-coder
Jun 07 2018 07:33
@Marmiz no there is an adapted version of ducks, re-ducks
I am just trying to understand how to plug in react router to it
Claudio Restifo
@Marmiz
Jun 07 2018 07:40
if it works similar to redux, why would you need to "plug" in react router?
you are facing the common issue of not re-rendering?
Francois Beyers
@Blusoup
Jun 07 2018 08:34
I hope someone has time to look at this and help me convert the following Cookie to sessionStorage. I would prefer to use sessionStorage instead of cookies, but they still confuse me.
function calculate_age(birth_year) {
        today_date = new Date();
        today_year = today_date.getFullYear();
        age = today_year - birth_year;
        return age;
    }

    var custAge = calculate_age(parseInt($('#birthYear').val()));
    var custGender = $('#gender').val();


    document.cookie = "mycookie_custAge=" + custAge + "; path=/;";
    document.cookie = "mycookie_custGender=" + custGender + "; path=/;";
    //console.log(custAge)
    //console.log(custGender);

//read cookie
function readCookie(k, r) { return (r = RegExp('(^|; )' + encodeURIComponent(k) + '=([^;]*)').exec(document.cookie)) ? r[2] : null; }

var v55 = readCookie('mycookie_custAge');

var v56 = readCookie('mycookie_custGender');
Claudio Restifo
@Marmiz
Jun 07 2018 08:51

@Blusoup
in order to write and read from storage (local or session):

localStorage.setItem('myKey', 'myValue') // will set a  "myKey": "myValue" pair into storage object
localStorage.getItem('myKey') // "myValue"

also be aware of the difference between localStorage and sessionStorage. The two are not equivalent

you can play around in your browser console if you want to. just type localStorage (or sessionStorage) to inspect what's there.
then you can try to set and get items
Chris
@ConnectCodes
Jun 07 2018 12:44
Hi guys

I am stuck at "Sass - Use @each to Map Over Items in a List” challenge:

<style type='text/sass'>
 @each $color in blue, black, red {
 .#{$color}-bg {background-color: $color;}
 } 


  div {
    height: 200px;
    width: 200px;
  }
</style>

<div class="blue-bg"></div>
<div class="black-bg"></div>
<div class="red-bg"></div>

In preview I see that I have background colors set but it still says blue/red/black-bg class should have a background-color of blue/red/black.Any ideas?

k_zone
@kzone0520
Jun 07 2018 13:34
Hello World
Nate Mallison
@NJM8
Jun 07 2018 14:20
@JLuboff @cmccormack Can you guys try it again? https://natethedev-votingappfrontend.herokuapp.com/ A few other people have been able to get on, not sure why you guys couldn't I did test a bit on Chrome but not Safari. If the app and api went to sleep (Heroku free dyno you know the deal) you may have to give it a few seconds to load.
I made a few ui improvements from other feedback, the only problem I'm aware of ATM is that when you delete one of your polls it deletes the wrong one, not sure what's up with that. Gotta hop off for a bit so I'll check it out later.
Shubham soni
@shubhamsoni2617
Jun 07 2018 14:25

Objective: Return true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.

let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: true
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function isEveryoneHere(obj) {
  // change code below this line
return 'Alan' in users==true && 'Jeff' in users==true && 'Sarah' in users==true && 'Ryan' in users==true;
  // change code above this line
}

console.log(isEveryoneHere(users));

Wondering if this challenge could be done more efficiently?
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property

Moisés Man
@moigithub
Jun 07 2018 14:32
return ['Alan', 'etc'].every(name=>name in users)
http://mdn.io/array.every
@shubhamsoni2617
Shubham soni
@shubhamsoni2617
Jun 07 2018 14:34
@moigithub Thanks a ton.
Moisés Man
@moigithub
Jun 07 2018 14:35
u could alsouse Object.keys to extract the names
Object.keys(users)
:point_right: ["Alan", "Jeff", "Sarah", "Ryan"]
Shubham soni
@shubhamsoni2617
Jun 07 2018 14:40
@moigithub picking up the keys from the object and comparing it with the same object seems quite awkward, what's your stake, provided I am thinking correctly?
let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: true
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function isEveryoneHere(obj) {
  // change code below this line
for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        return true;
    }
}
return false;
  // change code above this line
}

console.log(isEveryoneHere(users));
Moisés Man
@moigithub
Jun 07 2018 14:42
u returning too soon
remember return exit th function immediatelly... so.. as soon it found a matching key
it will exit ur function without chekcing the others
.... one way to fix ur code.. would be invert the condition... check for falsy and return sooner
(as soon as any of em not there.. u can return false -not every1 is there- )
also.. u should use the parameter provided... obj instead of users
Charlie Waite
@charliearlie
Jun 07 2018 14:47
Is there a bug on the task?
function isEveryoneHere(obj) {
  // change code below this line
  return Object.keys(obj).every(name => name in users);
  // change code above this line
}
That should work. As @moigithub pointed out
but it thinks it doesn't return false if they're not all in it. It does..
Shubham soni
@shubhamsoni2617
Jun 07 2018 14:49
@moigithub makes sense. Now, the only thing I am confused about is ,
Letting an array from the same object and comparing it with the keys?
Moisés Man
@moigithub
Jun 07 2018 14:49
dunno.. didnt read what the exercise is asking.. can u link it ?

Finish writing this function so that it returns true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.

ok.. so u need to work with all those 4 names.. always,, so no extract keys from the object..but hardcode it

Shubham soni
@shubhamsoni2617
Jun 07 2018 14:51
@charliearlie In nearly every other task, the test passes without giving accurate code.
Charlie Waite
@charliearlie
Jun 07 2018 14:51
So what I posted above actually passes if the object is empty
function isEveryoneHere(obj) {
  const keys = Object.keys(obj);
  if (keys.length === 0) return false;

  return keys.every(name => name in users);
}
That works
Moisés Man
@moigithub
Jun 07 2018 14:55
probably missing some test cases
Shubham soni
@shubhamsoni2617
Jun 07 2018 14:55
@moigithub hardcode meanwhile?
Moisés Man
@moigithub
Jun 07 2018 14:56
write the values directly on the code
return ["Alan", "Jeff", "Sarah", "Ryan"].every(name => name in obj);
Shubham soni
@shubhamsoni2617
Jun 07 2018 15:00
@moigithub Alright!
Am I thinking it correctly regarding letting an array from the same object and comparing it with the keys?
If you think I am unable to convey myself correctly, let me know. I will put it in some other words.
Blauelf
@Blauelf
Jun 07 2018 15:00
name in obj, not name in users
Moisés Man
@moigithub
Jun 07 2018 15:01
soo if u have some userlist like {Boo: {}, Bee: {}} should not pass @shubhamsoni2617
ye forgot to change.. copy paste
Shubham soni
@shubhamsoni2617
Jun 07 2018 15:04
@moigithub why so?
Moisés Man
@moigithub
Jun 07 2018 15:05

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/

so that it returns true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys

according to the instructions

Shubham soni
@shubhamsoni2617
Jun 07 2018 15:10
@moigithub Okay, Thanks.
Christopher McCormack
@cmccormack
Jun 07 2018 15:20
@NJM8 same issue - using Firefox
Jason Luboff
@JLuboff
Jun 07 2018 15:22
:wave:
Christopher McCormack
@cmccormack
Jun 07 2018 15:23
@JLuboff heya
Jason Luboff
@JLuboff
Jun 07 2018 15:23
Whats happening?
Christopher McCormack
@cmccormack
Jun 07 2018 15:24
@JLuboff nm man yourself?
Jason Luboff
@JLuboff
Jun 07 2018 15:24
Putting out :fire:
Christopher McCormack
@cmccormack
Jun 07 2018 15:24
@JLuboff forget to turn AC on in server room?
Jason Luboff
@JLuboff
Jun 07 2018 15:25
I actually did have to turn the AC on the other day lol We had a few power blips that killed it
Nah.. just helping people get setup on new computers. But we have a major internet circuit thats down right now which is no bueno..but I don't have to deal with that
Christopher McCormack
@cmccormack
Jun 07 2018 15:28
Well since you're talking to us sounds like your redundant connection(s) are still working :D
Jason Luboff
@JLuboff
Jun 07 2018 15:29
Nope. Don't think there's any redundant connections in place. Essentially we just took over an assest from a company that has gone bankrupt (like...literally just switched the circuit I think earlier this week) and they(ISP) disconnected the circuit becuase they got notification of the bankruptcy even though we had from my understanding switched it over to us
Christopher McCormack
@cmccormack
Jun 07 2018 15:32
carriers will do everything they can to mess up your day
Jason Luboff
@JLuboff
Jun 07 2018 15:32
So we'll get redudant connections put in I'm sure.. but.. I think its a total of 16 circuits
Christopher McCormack
@cmccormack
Jun 07 2018 15:33
We've had them accidentally disconnect our circuit instead of a completely different company, and they said they couldn't do anything
Aminu Ibrahim Abubakar
@AminuSufi585
Jun 07 2018 15:49
Hi there..
Any reader of Eloquent JavaScript here?
Jason Luboff
@JLuboff
Jun 07 2018 15:51
@AminuSufi585 I started reading it quite a while ago and stopped as I didn't care for the authors writing style
Aminu Ibrahim Abubakar
@AminuSufi585
Jun 07 2018 15:52
Ohh sorry.. But I find the book quite educative!
Have you tried You don't know JS then @JLuboff ?
Jason Luboff
@JLuboff
Jun 07 2018 16:04
I've read the first book of YDKJS and did enjoy it more but haven't found the time to get through the rest
Aminu Ibrahim Abubakar
@AminuSufi585
Jun 07 2018 16:06
Ohh ok
Are you currently using any book then? If yes which one? @JLuboff
Jason Luboff
@JLuboff
Jun 07 2018 16:06
No books.. just been building stuff :smile:
actually, I just finished a book..but its not a JS book, it's a T-SQL book hah. But started going through the new FCC material!
Aminu Ibrahim Abubakar
@AminuSufi585
Jun 07 2018 16:07
Great! I'coming along :)
Christopher McCormack
@cmccormack
Jun 07 2018 16:08
i read Javascript: The Good Parts and it was pretty good
short read too which is nice
Aminu Ibrahim Abubakar
@AminuSufi585
Jun 07 2018 16:08
I realized that, building stuffs helps much better...
I'll give it a try @cmccormack
Brad
@bradtaniguchi
Jun 07 2018 16:42
building stuff is always a better learning tool than reading/watching videos
Nate Mallison
@NJM8
Jun 07 2018 17:42
I would read YDKJS over The Good Parts anyday
read both, but The Good Parts doesn't teach you everything about JS, just teaches you to only use certain parts and avoid the rest and not take the time to understand it.
Nate Mallison
@NJM8
Jun 07 2018 17:51
@JLuboff I see your failed sign up in my heroku logs but not really sure how to figure out the issue from here
I assume the form validated your name/email/password right?
Christopher McCormack
@cmccormack
Jun 07 2018 17:59
yes form validation appears to be working. this is backend validation?
Nate Mallison
@NJM8
Jun 07 2018 17:59
ah
got it
he used test as a username, it should tell you not to use a duplicate but something there isn't working
Christopher McCormack
@cmccormack
Jun 07 2018 18:00
I also used test as a username :)
@JLuboff :fist:
Jason Luboff
@JLuboff
Jun 07 2018 18:01
Ah.. ya you gotta tell me its a dupe!
Nate Mallison
@NJM8
Jun 07 2018 18:02
well it did locally, testing again
I feel really proud of myself, I set up the front and back end to detect their environments so to test locally all I have to do is start them and all the api calls and cors access headers are set correctly to make local api calls, then when I push to prod it will look to the live api
Christopher McCormack
@cmccormack
Jun 07 2018 18:05
@NJM8 very nice!
what method did you use to prevent dupe votes?
Nate Mallison
@NJM8
Jun 07 2018 18:05
Screen Shot 2018-06-07 at 2.05.37 PM.png
hmmmm
why isn't that working in production
@cmccormack I use a voters array on each poll, if you are not logged in it adds your IP address to that list, if you are it adds IP Address plus your username, that way you can vote once unauthenticated and once for each username
front end checks that array before allowing the vote to post
Christopher McCormack
@cmccormack
Jun 07 2018 18:08
@NJM8 nice, just be wary many folks will be behind a NAT so they may not be able to vote unauthenticated if someone else in their network did
but I don't suppose our apps will really have to worry about that :D
@NJM8 there's something buggy with the poll graph with mouseover
@NJM8 try going to the framwork poll in FF and see if you can see it
Nate Mallison
@NJM8
Jun 07 2018 18:10
yeah, it has some downfalls. I thought of a good idea to actually create an account for each new user right as the app loads and generate a random number for the username, then use that until they actually sign up. then go through and change all the voters records to the new username. but I didn't want to do all that
yeah it shows your number of votes but the pie slice will move right?
if you refresh the page it goes away as the vote count is new, I'm not sure why it's doing that, I have to refresh the poll on vote so I would guess I need to destroy it first?
ah, I see chartsJS has an update method, I'll refactor to use that, my guess is you are seeing the new chart on top of the old one
Kudzu
@czhower
Jun 07 2018 18:14
NAT is a huge issue in such cases. Many apps use cookies or browser fingerprints. But browser fingerprints are going to become far more difficult. Apple is now working to block them, and there are GDPR issues wtih using them too.
Jason Luboff
@JLuboff
Jun 07 2018 18:14
If I remember correctly... the update/destroy method doesn't work (or atleast didn't for me) I had to destroy the container and create it again before displaying the graph
Kudzu
@czhower
Jun 07 2018 18:15
Email verficiation is the simplest and most reliable. Sure people can make new emails, but its harder than using incognito etc.. so its more reliable.
Nate Mallison
@NJM8
Jun 07 2018 18:17
is there any reason error messages from mongo would be different in production?
and how could I see those error message on Heroku? is there a heroku.log so it shows up in my logs or something?
Jason Luboff
@JLuboff
Jun 07 2018 18:18
There is a log in heroku
Its sort of hidden but its in there
Nate Mallison
@NJM8
Jun 07 2018 18:19
well I'm using papertrail but I'm not sure how to log errors from mongo
here is the signup route:
router
  .route('/signup')
  .post((req, res, next) => {
    db.Users.create(req.body).then(user => {
      const token = jwt.sign({ user_id: user.id }, process.env.SECRET_KEY, { expiresIn: 86400 })
      res.status(200).json({ idToken: token, expiresIn: 86400, message: 'Signed Up Successfully' })
    }).catch(error => {
      console.log(error)
      if (error.code === 11000) {
        if (error.message.split(' ').includes('email_1')) {
          return res.status(401).send('Email already taken')
        }
        if (error.message.split(' ').includes('username_1')) {
          return res.status(401).send('Username already taken')
        }
      }
      return next(error)
    })
  })
and the mongo error on duplicate username
message: 'E11000 duplicate key error collection: votingApp.users index: username_1 dup key: { : "nate" }',
so it must be different in production. hmmm, maybe there are no errors because it's in production
Christopher McCormack
@cmccormack
Jun 07 2018 18:26
Do you have your errors bound to a dev variable?
Moisés Man
@moigithub
Jun 07 2018 18:26
console.log(req.body)
check what have "nate" in it..
Christopher McCormack
@cmccormack
Jun 07 2018 18:26
@czhower that's true but this project specifically states unauthenticated users can also vote
Nate Mallison
@NJM8
Jun 07 2018 18:28
I guess I have to do a find for the username and the email, I recall now doing that in other projects
huh, nope, I did the same error check in my other app
Brad
@bradtaniguchi
Jun 07 2018 18:39
thank god for testing, beating my head on some basic boolean logic math, but with 15 test cases backing me up I was able to fix it, thank god :D
Jason Luboff
@JLuboff
Jun 07 2018 18:39
Good job
Nate Mallison
@NJM8
Jun 07 2018 18:44
ah
the error messages are different in production
@JLuboff fixed
Nate Mallison
@NJM8
Jun 07 2018 18:58
@cmccormack when you get a chance can you see if that weirdness on the graph is fixed? I don't think their update method did it, something else weird is going on
Christopher McCormack
@cmccormack
Jun 07 2018 19:08
@NJM8 Appears to be better now
Nate Mallison
@NJM8
Jun 07 2018 19:11
I tried it after I updated it and there is some weirdness, like on your icecream poll if you mouse over the empty area then then mouse on top of the poll with lower votes it will jump up and the scale with adjust to show them at the same vote.
Christopher McCormack
@cmccormack
Jun 07 2018 19:11
@NJM8 yeah that's what I was seeing
Nate Mallison
@NJM8
Jun 07 2018 19:12
I'll check it again tomorrow, thanks for taking a look
Christopher McCormack
@cmccormack
Jun 07 2018 19:18
np
Jason Luboff
@JLuboff
Jun 07 2018 20:07
@NJM8 Just curious...why can't I view polls without being logged in?
Also, unless they've changed it, I should be able to add an additional option for a poll, but I don't see that ability?
Another item I'll point out.. I was able to sign up with Test as my username, even though a test user already exists
Nate Mallison
@NJM8
Jun 07 2018 22:13
@JLuboff you should be able to see polls and vote while logged out, let me know if you have any network 500's. I haven't added the option to add options to polls. I need to review the poll template to see how to do it, I wasn't thinking about it from the start and it's going to take some work.
Jason Luboff
@JLuboff
Jun 07 2018 22:15
@NJM8 Hmm...now I can see the polls without logged in. Wierd
Nate Mallison
@NJM8
Jun 07 2018 22:17
maybe it just had issues after the log in issue, it should populate the polls from the DB when you open the app, also it's heroku so maybe the dyno was asleep and slow to respond
Jason Luboff
@JLuboff
Jun 07 2018 22:18
It had occured prior to me creating an account (and now I'm not logged in either though)
Nate Mallison
@NJM8
Jun 07 2018 22:18
weird. idk. maybe last time you happened to open it when I was building a new version, it takes a couple minutes