These are chat archives for FreeCodeCamp/HelpJavaScript

25th
Jan 2018
shigaoping
@shigaoping
Jan 25 2018 00:56 UTC

var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};

function checkObj(checkProp) {
var result;
switch(myObj.hasOwnProperty("checkProp")){
case true: result=myObj[checkProp];break;
case false:result="Not Found";break;
}
return result;
}

checkObj("gift");

//where is the problem?
Jason Luboff
@JLuboff
Jan 25 2018 00:58 UTC
@shigaoping Don't use quotews around checkProp
shigaoping
@shigaoping
Jan 25 2018 00:59 UTC
You're right.
Diego Mayer
@Chrono79
Jan 25 2018 02:13 UTC
@shigaoping I think switch is an overkill for only 2 choices
Conor Murphy
@cmurphy580
Jan 25 2018 03:20 UTC
I've go a problem with a setInterval function that increments through an array/object of pictures. The first iteration through the array glitches when it switches to the next picture, but then after that is a smooth transition. Here's an example of what I'm talking about:
https://cmurphy580.github.io/eCommerce-Site-_-FjallRaven/
Does anyone know how to fix this issue?
Dan Lafferty
@DanLaff
Jan 25 2018 03:28 UTC
@cmurphy580 the images need to be cached first - you can use onload to process all the images. Once all are loaded, fire off your setInterval function.
Conor Murphy
@cmurphy580
Jan 25 2018 03:31 UTC
@DanLaff so do i need to do a setTimeout then call the interval
Dan Lafferty
@DanLaff
Jan 25 2018 03:32 UTC
@cmurphy580 It would be something like:
const imageURLs = // an array of image URLs
imageURLs.forEach(url => {
  const img = new Image();
  img.src = url;
  img.onload = () => {
    // append the image to your page
    // check how many images have been loaded
    // when the number of images appended == the number of URLs in your array, start the carousel
  };
});
Conor Murphy
@cmurphy580
Jan 25 2018 03:35 UTC
@DanLaff alright i'll start working with that setup. Thanks for the help
CamperBot
@camperbot
Jan 25 2018 03:35 UTC
cmurphy580 sends brownie points to @danlaff :sparkles: :thumbsup: :sparkles:
api offline
Aniculaesei
@Aniculaesei
Jan 25 2018 05:23 UTC
Hi guys!What IDE do you use for coding? :)
Thank you! :)
rukkho
@rukkho
Jan 25 2018 05:27 UTC
Hi guys!
I'm a newbie in JavaScript seeking for your help. And I'm happy YOU are there for me. Thanks
Sweet Coding :)
@SweetCodingInc
Jan 25 2018 07:05 UTC
@Aniculaesei Depends on what tech you want to code in
alpox
@alpox
Jan 25 2018 09:15 UTC
@Aniculaesei For javascript, Visual Studio Code
Alexander Køpke
@alexanderkopke
Jan 25 2018 09:48 UTC
@rukkho hey. No problem :)
Claudio Restifo
@Marmiz
Jan 25 2018 10:10 UTC
Is VSCode an IDE? I've always thought that was an editor :thought_balloon:
alpox
@alpox
Jan 25 2018 10:11 UTC
@Marmiz Its something in between. Its mostly a texteditor, but gets more and more IDE features
The microsoft team has a lot of experience with IDE creation and so it brings in lots of features of it
So you can do debugging, you can jump around in code and definitions, inspect stuff, have snippets, lots of extensions which provide whatever functionality... etc.
Now you can even open multiple projects at once. You also have an integrated terminal
Claudio Restifo
@Marmiz
Jan 25 2018 10:14 UTC
But that stuff comes built-in or plugged in?
out of curiosity... I downloaded it and never opened it :)
alpox
@alpox
Jan 25 2018 10:15 UTC
@Marmiz Everything that i just said comes built-in
But there are also lots of extensions ofc (Which can be simply added within the editor itself)
Claudio Restifo
@Marmiz
Jan 25 2018 10:17 UTC
wow :open_mouth:
that's impressive :)
I should definitely check it out
alpox
@alpox
Jan 25 2018 10:18 UTC
Go ahead, and don't look back ;-)
Claudio Restifo
@Marmiz
Jan 25 2018 10:21 UTC
well it's that after a while I'm so used to tmux - sh - and vim workflow.
I know that there's a VSCode Vim mapping, so maybe can be worth a try
alpox
@alpox
Jan 25 2018 10:22 UTC
Hmm the last time i tried it it was not so good though
I prefer the real vim if i need vim :D
But with VSCode i just have more overview over my project and so i just drop the vim movement for better overview - thats my tradeoff.
Claudio Restifo
@Marmiz
Jan 25 2018 10:31 UTC
Make sense, I'll give it a try for sure! :)
(p.s. try tmux if you have never tried it - so worth)

lol, there's even a plugin for VSCode tmux.
https://github.com/elken/mux

I'm sold!

alpox
@alpox
Jan 25 2018 10:36 UTC
I actually haven't used tmux yet but as far as i remember its only a terminal multiplexer
If i once need many terminal windows i'll come back to it :D
Muhammad Hasham
@MohammadHasham
Jan 25 2018 14:24 UTC
i just wanted to clear my concept that we use serialize and deserialize in passport, so do we still need to store the user id in cookies?
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 14:54 UTC

"In a typical web application, the credentials used to authenticate a user will only be transmitted during the login request. If authentication succeeds, a session will be established and maintained via a cookie set in the user's browser.

Each subsequent request will not contain credentials, but rather the unique cookie that identifies the session. In order to support login sessions, Passport will serialize and deserialize user instances to and from the session"

on the deserialize you choose what to store
Aniculaesei
@Aniculaesei
Jan 25 2018 15:01 UTC
Who is from Romania around hereee??? :)
Johnny
@JohnnyBizzel
Jan 25 2018 15:02 UTC
Gheorgi Hagi!
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:03 UTC
https://www.codewars.com/kata/double-cola/train/javascript/59b8e919623654184600005b can smeone give em a little tip on this? from my calculations the whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 52)=="Penny" get's me leonard and not penny any idea?
since we are supposed to double it every time
2 2 2 2 2
4 4 4 4 4
8 8 8 8 8
16 16 16 16
Johnny
@JohnnyBizzel
Jan 25 2018 15:06 UTC
@tiagocorreiaalmeida Is this a beta kata? They didn't write any tests.
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:06 UTC
there are tests but they dont give you acess to them the one pointed there comes on the kata info
Johnny
@JohnnyBizzel
Jan 25 2018 15:08 UTC
@tiagocorreiaalmeida Looks like you need a while loop. At least most people use that.
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:09 UTC
my point is based on the idea the 52 cola should be to leonard and not penny So I guess my thinking is wrong was trying to figure out that before write even a single line
Johnny
@JohnnyBizzel
Jan 25 2018 15:10 UTC
@tiagocorreiaalmeida I didn't really understand the question!
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:11 UTC
they say that with the names above the 52 coca cola will be for penny
they also say that everytime someone takes a cola from the vending machine they double it
im trying to figure out why does the 52 coca cola doesnt go for leonard
I showed above the numbers example
Johnny
@JohnnyBizzel
Jan 25 2018 15:13 UTC
@tiagocorreiaalmeida 51 will get Leonard. Remember arrays are 0 indexed
Stephen James
@sjames1958gm
Jan 25 2018 15:14 UTC
@tiagocorreiaalmeida 5, 10, 20 people in line, so after 35 you start over with 8 each 35 + 16 is 51, the end of leonard, so 52 is the first penny
@tiagocorreiaalmeida
1 1 1 1 1  - 5
2 2 2 2 2 - 15 total
4 4 4 4 4 - 35 total
8 8 - 51 total
8 8 8
16 16 16 16
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:15 UTC
guess so :D
ahahah
thanks
Diego Mayer
@Chrono79
Jan 25 2018 15:37 UTC
@tiagocorreiaalmeida now you have 1 follower in CW :P
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:41 UTC
about to get famous better step back
:D
Diego Mayer
@Chrono79
Jan 25 2018 15:47 UTC
@tiagocorreiaalmeida a word of advice, the js version of that kata doesn't use the same array for the random tests
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 15:49 UTC
yeha I had a strange soltuion was similar to recursion and pushing and pushing values into the array nthil the n cola, but there's an easier way that im going to implement now and that doenst times out :D
Michael Grienauer
@mgrienauer
Jan 25 2018 16:01 UTC
i have a question about closures.. if you assign a function to a variable, does that variable essentially act as a pointer? and it prevent the function from being deleted in memory as the program runs?
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 16:06 UTC
@Chrono79 ready to see a really strange idea?:D
function whoIsNext(names, r){
  if(r <= names.length) return names[r-1];
  let positions = new Array(names.length).fill(1);
  count = 2;
    while(r > count / names.length){
        for(let i = 0; i < positions.length; i++){
          positions[i] += count;
          if(positions.reduce((a,b)=>a+=b) >= r)return names[i]
        }
        count +=count;
    }
}
Diego Mayer
@Chrono79
Jan 25 2018 16:08 UTC
where are you returning if r > names.length?
Does it work?
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 16:10 UTC
if(positions.reduce((a,b)=>a+=b) >= r)return names[i]
Jason Luboff
@JLuboff
Jan 25 2018 16:10 UTC
Morning :wave:
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 16:11 UTC
hey!!
Diego Mayer
@Chrono79
Jan 25 2018 16:12 UTC
@tiagocorreiaalmeida Have to go now, I'll take a look later, it seems strage indeed :P
Jason Luboff
@JLuboff
Jan 25 2018 16:17 UTC
@tiagocorreiaalmeida What up
Tiago Correia
@tiagocorreiaalmeida
Jan 25 2018 16:54 UTC
all good just working how about you?
Kaz Baig
@kbaig
Jan 25 2018 16:55 UTC
@mgrienauer I like this answer when it comes to the differences between function declaration and variable assignment https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname
Jason Luboff
@JLuboff
Jan 25 2018 16:55 UTC
@tiagocorreiaalmeida Same. heh
Markus Kiili
@Masd925
Jan 25 2018 18:14 UTC
@mgrienauer Yes.
Jason Luboff
@JLuboff
Jan 25 2018 18:25 UTC
@Masd925 No.
Markus Kiili
@Masd925
Jan 25 2018 18:27 UTC
:cry:
Jason Luboff
@JLuboff
Jan 25 2018 18:28 UTC
@Masd925 MAybe.
jusgoose
@gusd773
Jan 25 2018 18:55 UTC

help?? this works except for when I have duplicates :( "where do i belong challenge"
/*

function getIndexToIns(arr, num) {
var indexTracker = 0;
var sortArr = arguments[0].sort();
for (var i = 0; i<sortArr.length; i++){ if (num>sortArr[i]){
indexTracker = indexTracker+1;
}
else {
return indexTracker;
}
}

}

getIndexToIns([5,3,20,3], 5);
*/

sabahatullah
@sabahatullah
Jan 25 2018 18:56 UTC
Hi could someone please look into this function. I basically want to return some value on the whole site and some additional value on specific pages
jusgoose
@gusd773
Jan 25 2018 18:56 UTC
also I've tried >= in the if statement and actually takes gives more red x's in checkmarks list
Diego Mayer
@Chrono79
Jan 25 2018 19:05 UTC
@gusd773 your sorting is missing a callback function to sort numbers right
Kaz Baig
@kbaig
Jan 25 2018 19:05 UTC
yeah it is
sabahatullah
@sabahatullah
Jan 25 2018 19:07 UTC
Let me know if returning values like this is a proper way. (codepen link above)
I basically want to return some value on the whole site and some additional value on specific pages
Dan Lafferty
@DanLaff
Jan 25 2018 19:11 UTC
@sabahatullah you are returning early, before your two if statements. Aside from that, it looks fine
sabahatullah
@sabahatullah
Jan 25 2018 19:12 UTC
the one I am returning early, I want to return on ALL the pages
return {
"profile": {
CRO01: readCookie("CRO01"),
CRO02: readCookie("CRO02")
}
}
the ones under if statement would be additional values I want to return for those specific pages
Dan Lafferty
@DanLaff
Jan 25 2018 19:13 UTC
Make that into a variable and combine it with your other returns
return can only run once in JS
sabahatullah
@sabahatullah
Jan 25 2018 19:14 UTC
so one those pages for example it would become
"profile": {
CRO01: readCookie("CRO01"),
CRO02: readCookie("CRO02")
},
"entity": {
id: utag_data.product_id,
}
@DanLaff could you tell me how
"Make that into a variable and combine it with your other returns"
Dan Lafferty
@DanLaff
Jan 25 2018 19:19 UTC
@sabahatullah You can use Object.assign to combine two objects
  const defaultObj = {
    profile: {
      CRO01: readCookie("CRO01"),
      CRO02: readCookie("CRO02")
    }
  };

  // product listing page
  if (document.URL.indexOf("/l/") > -1) {
    return Object.assign(defaultObj, {
      entity: {
        id: utag_data.product_id,
        categoryId: utag_data.product_subcategory
      }
    });
  }
sabahatullah
@sabahatullah
Jan 25 2018 19:23 UTC
nice one let me give it a shot.
sabahatullah
@sabahatullah
Jan 25 2018 19:30 UTC
works very nicely. @DanLaff thanks
CamperBot
@camperbot
Jan 25 2018 19:30 UTC
sabahatullah sends brownie points to @danlaff :sparkles: :thumbsup: :sparkles:
api offline
sabahatullah
@sabahatullah
Jan 25 2018 19:30 UTC
what is return Object.assign though?
Dan Lafferty
@DanLaff
Jan 25 2018 19:31 UTC
@sabahatullah It's the same as doing:
const newObject = Object.assign(defaultObj, {
  entity: {
    id: utag_data.product_id,
    categoryId: utag_data.product_subcategory
  }
});

return newObject;
Not sure if that's your question
sabahatullah
@sabahatullah
Jan 25 2018 19:36 UTC
perfect yes. its good to know efficient ways of doing thing in few lines as opposed to just putting loads of if conditions with repetitive code
sabahatullah
@sabahatullah
Jan 25 2018 19:41 UTC
@DanLaff many thanks
CamperBot
@camperbot
Jan 25 2018 19:41 UTC
sabahatullah sends brownie points to @danlaff :sparkles: :thumbsup: :sparkles:
api offline
Dan Lafferty
@DanLaff
Jan 25 2018 19:56 UTC
:thumbsup:
Daniel Romero
@Ranacode
Jan 25 2018 21:41 UTC
I have this test and I want to check the three possibilities that cannot be the property, the thing is I don't want to check for it three times, how can I achieve this?
expect(res.body).to.have
                        .property('errors')
                        .to.have.own.property('github')
                        .but.not.own.property(/^(author|description|title)$/);