These are chat archives for FreeCodeCamp/HelpJavaScript

11th
May 2018
Christopher McCormack
@cmccormack
May 11 2018 00:15
@JLuboff later!
Christopher
@bradley1492
May 11 2018 11:14

Hello guys, I have got another question regarding the Inventory Update challenge.
With the following code:

for (const x of arr2){
  if (arr1.every(function check(element){
    return element != arr2[x][1];
  }))
  {
      arr1.push(x);
     }
}

Is the x variable of the for loop possible to be read inside the execution of the .every method?
Like arr2[x][1];?

Stephen James
@sjames1958gm
May 11 2018 11:38
@bradley1492 No x is arr2[?] of each index ?
So x[1]
Also if arr1 has the same types of values it would be
element[1] !== x[1]
Christopher
@bradley1492
May 11 2018 12:13
@sjames1958gm Thanks now it worked, but with a false result.
I think there must be something wrong with how element[1] is being compared to every first element in the arr2 arrays...
In the arr1.every callback doesn't it only return true if arr2[x][1] is not identical to any first position of the element arrays?
CamperBot
@camperbot
May 11 2018 12:13
bradley1492 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 9239 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Christopher
@bradley1492
May 11 2018 12:14
@sjames1958gm I changed the code to this:
https://codepen.io/bradley1492/pen/qYpgpG
Stephen James
@sjames1958gm
May 11 2018 12:15
@bradley1492 element[1] is the value to compare?
Christopher
@bradley1492
May 11 2018 12:17
@sjames1958gm I want to compare each first element of every array in arr2 with every first element of the arrays in arr1
I hope that is clear, is it?
Stephen James
@sjames1958gm
May 11 2018 12:18
[21, "Bowling Ball"], this is what element is so you need to use element[1] to get the string
Christopher
@bradley1492
May 11 2018 12:18
@sjames1958gm oh sorry I think i forgot to update codepen. Here's the current version:
https://codepen.io/bradley1492/pen/qYpgpG
Blauelf
@Blauelf
May 11 2018 12:19
x[1], not [x][1] (which is undefined)
Christopher
@bradley1492
May 11 2018 12:21
@Blauelf thx I think it worked finally
CamperBot
@camperbot
May 11 2018 12:21
bradley1492 sends brownie points to @blauelf :sparkles: :thumbsup: :sparkles:
:star2: 4694 | @blauelf |http://www.freecodecamp.org/blauelf
Christopher
@bradley1492
May 11 2018 12:21
:)
Stephen James
@sjames1958gm
May 11 2018 12:22
@Blauelf Hey.
@Blauelf Needed another set of eyes, I wasn't seeing that one
Christopher
@bradley1492
May 11 2018 12:22
with [x][1] .every always returns true and hence always pushes x to arr1?
Stephen James
@sjames1958gm
May 11 2018 12:22
@bradley1492 exactly
Christopher
@bradley1492
May 11 2018 12:23
@sjames1958gm yes thanks problem solved then :)
CamperBot
@camperbot
May 11 2018 12:23
bradley1492 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
api offline
Blauelf
@Blauelf
May 11 2018 12:44
@bradley1492 I think I recognized this only because it is part of my card counting solution, in the form of
return (count += (card < 7) - !(card < 10)) + [" Hold", " Bet"][+(count > 0)];
//                                              ^ there, same structure, array literal with index
Christopher
@bradley1492
May 11 2018 12:46
@Blauelf oh ok hm I can't reallyyy see the same structure here hahahah :)
Blauelf
@Blauelf
May 11 2018 12:47
There have been programming languages that used distinct symbols for about anything (for example APL). But that requires a ton of special keys ;)
I think in JS, the multiple uses of {} and : are worse than those of []. Or at least as bad.
Nate Mallison
@NJM8
May 11 2018 13:04
Question about es6 import statement, what is the difference between this import { something} from './something.js'; and this import something from './something.js';
nvm
Christopher
@bradley1492
May 11 2018 13:08
@Blauelf Hm I just thought with time one really gets good at reading more sophisticated structures very quick...
Obviously I can't compare, since I never looked at anything else than js
Blauelf
@Blauelf
May 11 2018 13:10
Well, declaring an array and taking one of its elements (discarding the array as it's not referenced anywhere) is not exactly good code, so you would not see that often.
@NJM8 I'd assume import something from './something.js' to import the default export and store it in variable something, while { something } would import the export named "something" and store it in a variable of same name.
Guangyao
@guoguangyao
May 11 2018 13:56
check
ehutchllew
@ehutchllew
May 11 2018 14:01
@NJM8 @Blauelf Blauelf is correct
Blauelf
@Blauelf
May 11 2018 14:03
The module syntax is so confusing. It looks like an object literal on export, but isn't. It looks like object destructuring on import, but isn't.
Nate Mallison
@NJM8
May 11 2018 14:47
Yeah after doing lots of work with node require then doing some tutorials that use es6 imports I wonder why it needs to be so complex. just expose and use the functions
RicardoLFoco
@RicardoLFoco
May 11 2018 15:02
Has anyone here been through the Arguments Optional - Intermediate Algorithm? I feel like i'm really close
Nate Mallison
@NJM8
May 11 2018 15:05
yes
post your code?
RicardoLFoco
@RicardoLFoco
May 11 2018 15:07

addTogether(2)(3) addTogether(2)([3])

These two are causing problems with my code

function addTogether() {
    function checkArguments(array){
        for (var element of array) {
            if (typeof element !== "number") return undefined;
        }
        return true;
    }

    function reducer(accumulator, currentValue){
        return accumulator + currentValue;
    }

    var args = [...arguments];
    console.log(args);
    if (checkArguments(args)){
        if (args.length == 2){
            sum = args.reduce(reducer);
            return sum;
        } else {
            var sumOldNumAnd = Function(addTogether(args[0]));
            return sumOldNumAnd;
        }
    } else {return undefined};
  }

  addTogether(2)([3]);
Nate Mallison
@NJM8
May 11 2018 15:08
arguments optional can be a tough one, need to understand closures and partial application
RicardoLFoco
@RicardoLFoco
May 11 2018 15:09
So the console.log(args) is causing a maximum call stack error. The passed in parameters are weird with the () and I'm not sure how to go about retrieving them
Blauelf
@Blauelf
May 11 2018 15:11
@RicardoLFoco With Function(addTogether(args[0])), I guess you meant addTogether.bind(null, args[0]).
Nate Mallison
@NJM8
May 11 2018 15:12
the (2)([3]) is the same as saying
Jason Luboff
@JLuboff
May 11 2018 15:12
Morning (afternoon, evening)
Nate Mallison
@NJM8
May 11 2018 15:13

var sumTwoAnd = addTogether(2);
 console.log(sumTwoAnd(3));
you pass your function the 2, then it returns the function since there is only one arg, and runs the return function immediately with the arg [3]
Blauelf
@Blauelf
May 11 2018 15:13
Looks as if the first of those two lines runs in an infinite loop and therefore call stack exceeded.
RicardoLFoco
@RicardoLFoco
May 11 2018 15:14

So first I am making the arguments object into an array (args)

Then calling checkArguments(args) -- which loops through args and returns undefined if one of the elements has typeof that is not "number" otherwise returns true -- If checkArguments(args) returns true -->

if the length of args is equal to 2 -- use .reduce() to get a sum using reducer
return sum

else return the variable that is supposed to be addtogether with one parameter already passed in. No idea if I am doing that right -- FreeCodeCamp is telling me that "eval() can be dangerous"

Blauelf
@Blauelf
May 11 2018 15:14
@RicardoLFoco Using Function constructor is as bad as eval. It lets you create functions from strings.
Nate Mallison
@NJM8
May 11 2018 15:15
@RicardoLFoco you have it right up until your else statement
    } else {
        var sumOldNumAnd = Function(addTogether(args[0]));
        return sumOldNumAnd;
    }
you don't want to return a call to your own function, you want to return a different function, one that holds the first arg passed in the first call, and is expecting another num to add to that
RicardoLFoco
@RicardoLFoco
May 11 2018 15:16
Yeah this is the part I don't understand along with the weird parameters of addTogether(2)(3) addTogether(2)([3])
Blauelf
@Blauelf
May 11 2018 15:17
That Function(addTogether(args[0])); calls the function itself, which creates the infinite loop (-> call stack exceeded). One could use partial application instead, using bind
Nate Mallison
@NJM8
May 11 2018 15:17
that is why you are getting an infinite loop, your function is designed to return a function if it sees one arg, so by returning your function again you are recursively automatically calling it
so in that else statement, it's key to think, I already have the first arg (2), I don't need to check it for number of args, want i want to do is make sure I'm passed a number again and add it to the first one
Blauelf
@Blauelf
May 11 2018 15:19
Or return a completely different function created within your function. But what you're currently attempting is partial application, which can best be emulated using bind :point_up: 11. Mai 2018 17:11
Nate Mallison
@NJM8
May 11 2018 15:19
remember addtogether(2)([3]) is the same as
var fn = addTogether(2);
fn(3);
Blauelf
@Blauelf
May 11 2018 15:20
fn([3]), don't forget that's an array ;)
RicardoLFoco
@RicardoLFoco
May 11 2018 15:21
Well if console.log(args[1]) for addtogether(2)([3]) -- it returns undefined
args[0] does return 2
It seems like really odd syntax
Nate Mallison
@NJM8
May 11 2018 15:23
huh, now that I look at it my solution doesn't account for the array, but seems to have passed the test
yeah no array in the challenge
RicardoLFoco
@RicardoLFoco
May 11 2018 15:24
It's two separate arguments. So there would be two arguments objects?
Nate Mallison
@NJM8
May 11 2018 15:24
"For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function."
Blauelf
@Blauelf
May 11 2018 15:24
@RicardoLFoco In two different functions. The one called first, and the one returned by the first and called second.
Nate Mallison
@NJM8
May 11 2018 15:25
@RicardoLFoco take a step back, your function returns 5 if you pass 2 + 3 right?
but if you pass only 2, it returns a function expecting another number, so you pass that number 3 and get 5 back
RicardoLFoco
@RicardoLFoco
May 11 2018 15:26
Yes it passes : addTogether(2, 3), addTogether("http://bit.ly/IqT6zt"), addTogether(2, "3")
Nate Mallison
@NJM8
May 11 2018 15:27
addTogether(2)(3) is TWO function calls, the first (2) returns a function which is run immediately by (3)
read that about three times haha
RicardoLFoco
@RicardoLFoco
May 11 2018 15:28
Alright man
Nate Mallison
@NJM8
May 11 2018 15:29
so the challenge is to make a closure, which is a function B returned from a function A that holds the variables from A
so write it out, in the else, return a function that adds the argument it receives to the argument from the first function
RicardoLFoco
@RicardoLFoco
May 11 2018 15:34
I see what you mean. Now that I look at it though addTogether(2)([3]) "should return undefined" - Is what it says
Nate Mallison
@NJM8
May 11 2018 15:36
ha, what happens if you run addTogether(2)(3)?
RicardoLFoco
@RicardoLFoco
May 11 2018 15:41
function sumOldNumAnd(num) {
      return num + args[0];
    }

    if (checkArguments(args)){
        if (args.length == 2){
            sum = args.reduce(reducer);
            return sum;
        } else {
            return sumOldNumAnd(num);
        }
    } else {return undefined};
That's my updated code. I don't see how the 3 is to be accessed
With addTogether(2)(3) --> args.length is 1 (2 the first param)
Nate Mallison
@NJM8
May 11 2018 15:43
what happens if you run it?
RicardoLFoco
@RicardoLFoco
May 11 2018 15:43
So num here would be the 3 but without using the arguments object?? I feel like I'm missing something
It still doesn't work
Moisés Man
@moigithub
May 11 2018 15:44
check about currying
Nate Mallison
@NJM8
May 11 2018 15:44
you are but not what you think
you are very close, rather than defining a function and returning it in your else statement, just return the function
RicardoLFoco
@RicardoLFoco
May 11 2018 15:45
But that created a call stack error when the function called itself
Nate Mallison
@NJM8
May 11 2018 15:46
so
else {
 return function(num){
  return args[0] + num;
}
}
that was before when you returned Function addTogether
RicardoLFoco
@RicardoLFoco
May 11 2018 15:47
Yeah but function declarations aren't supposed to be placed in blocks
according to FCC
Nate Mallison
@NJM8
May 11 2018 15:47
uhhh, where do you read that?
RicardoLFoco
@RicardoLFoco
May 11 2018 15:47
FCC
Nate Mallison
@NJM8
May 11 2018 15:48
well, then let's take a look at closures which is what you are doing, when you write a function you define a new scope, a closure is a function inside another that has scope over the outer functions variables.
the code you have now:
} else {
return sumOldNumAnd(num);
}
will not work because the function declaration is outside of the the function calling it, it only has access to what you pass it
RicardoLFoco
@RicardoLFoco
May 11 2018 15:50
what I'm really confused about here is how to access the 3 which in the case would be num. How to access the 3 without using the arguments object which I guess isn't working here
I see what you mean
Nate Mallison
@NJM8
May 11 2018 15:50
and you cannot pass it arg[0] because later on when it gets run by (3) then arg[0] no longer exists
so the function you return has to be defined inside the function addTogether, or else it doesn't know what arg[0] is. I'm not sure where you read function declarations must be outside other function but that is not correct. For this closure to work you must create and return the anonymous function closure from inside the other one
RicardoLFoco
@RicardoLFoco
May 11 2018 15:53
if (checkArguments(args)){
        if (args.length == 2){
            sum = args.reduce(reducer);
            return sum;
        } else {
            var oldNum = args[0];
            function sumOldNumAnd(num){
              return oldNum + num;
            }
        }
    } else {return undefined};
Closer?
Nate Mallison
@NJM8
May 11 2018 15:55
run it you tell me :smile:
RicardoLFoco
@RicardoLFoco
May 11 2018 15:55
Nate, it won't work.
Nate Mallison
@NJM8
May 11 2018 15:56
two more steps
it's anonymous, don't give it a name
well you can but don't need to
but you aren't returning it
RicardoLFoco
@RicardoLFoco
May 11 2018 15:56
Alright, now you're just messing with me
Nate Mallison
@NJM8
May 11 2018 15:56
    } else {
        var oldNum = args[0];
        return  function sumOldNumAnd(num){
          return oldNum + num;
        }
    }
Kudzu
@czhower
May 11 2018 15:57

OMG:
IDIOTS running the asylum... PATCH is case senstive to Chrome and FF, but other HTTP methods are not and the HTTP protocol isnt either.

http://www.brendanforster.com/blog/case-sensitive.html

Just lost nearly 24 hours to this garbage
RicardoLFoco
@RicardoLFoco
May 11 2018 15:57
Sure, but how do i get the 3??
Nate Mallison
@NJM8
May 11 2018 15:57
@RicardoLFoco you don't have to name it, but it well help with debugging as the name will show in the stack trace
you already are mate
addTogether(2) returns a function sumOldNumAnd(num), then the (3) calls that function immediately
run it
RicardoLFoco
@RicardoLFoco
May 11 2018 16:00
Okay it works
for that one anyway. Not the one with the array
Nate Mallison
@NJM8
May 11 2018 16:00
@czhower that's f*ed up
@RicardoLFoco great! all you need to do now is verify you are getting a number in your sumOldNumAnd function

and do you understand how it works? it helps to come back to this:

var fn = addTogether(2);
console.log(fn(3)); // 5

So we get one arg, return a function which is now stored in fn, this func is a closure with the 2 stored, then we run that stored function fn(3) and it returns 5

RicardoLFoco
@RicardoLFoco
May 11 2018 16:03
if (checkArguments(args)){
        if (args.length == 2){
            sum = args.reduce(reducer);
            return sum;
        } else {
            var oldNum = args[0];
            return function sumOldNumAnd(num){
              if (typeof num !== "number") {
                return undefined;
              }
              return oldNum + num;
            };
        }
    } else {return undefined};
Final code -- It works!
Kudzu
@czhower
May 11 2018 16:03
@NJM8 Very. This is the kind of lax stuff I was talking about.
Moisés Man
@moigithub
May 11 2018 16:03
or call addtogether function again with oldNum , num
since it already have arguments check implemented
RicardoLFoco
@RicardoLFoco
May 11 2018 16:03
@moigithub That creates a call stack error
Moisés Man
@moigithub
May 11 2018 16:04
} else {
            var oldNum = args[0];
            return function sumOldNumAnd(num){
                    return addtogether(oldNum , num);
            };
        }
like that ??
RicardoLFoco
@RicardoLFoco
May 11 2018 16:04
Maybe, i'll try it
Nate Mallison
@NJM8
May 11 2018 16:05
won't work
RicardoLFoco
@RicardoLFoco
May 11 2018 16:06
No it does
Nate Mallison
@NJM8
May 11 2018 16:06
hmm, I tested mine wrong then, let me try again
oh duh, I wrote return addTogether(args[0] + num);
der der
Moisés Man
@moigithub
May 11 2018 16:08
:)
Nate Mallison
@NJM8
May 11 2018 16:09
I wanted you to understand the closure as a stand alone before getting more complex. closures and partial application are super powerful, although we use closures all the time without thinking about it
glad you got it
.bind would also work as suggested above is it binds the scope with those variables to your function call
RicardoLFoco
@RicardoLFoco
May 11 2018 16:13
@NJM8 Thanks for your help I really appreciate it. The FCC window one works in (little alerts of the IDE whatever) say not have function declaration in blocks, so I will have to read up on if thats actual practice as well as closures and partial application. Lots to learn!
CamperBot
@camperbot
May 11 2018 16:13
ricardolfoco sends brownie points to @njm8 :sparkles: :thumbsup: :sparkles:
:cookie: 432 | @njm8 |http://www.freecodecamp.org/njm8
Nate Mallison
@NJM8
May 11 2018 16:20
your welcome, can you screen shot that error so I can see? I generally ignore the FCC ide errors and just write whatever I want, haha
Christopher McCormack
@cmccormack
May 11 2018 16:21
@RicardoLFoco did you give it a name when you created it?
Jason Luboff
@JLuboff
May 11 2018 16:23
@cmccormack Yo
Christopher McCormack
@cmccormack
May 11 2018 16:24
@JLuboff heya
Jason Luboff
@JLuboff
May 11 2018 16:24
Whats good
Christopher McCormack
@cmccormack
May 11 2018 16:24
Trying to get my voting app hosted on my digital ocean server but running into issues
Jason Luboff
@JLuboff
May 11 2018 16:25
Lame
Christopher McCormack
@cmccormack
May 11 2018 16:25
Yeah
Jason Luboff
@JLuboff
May 11 2018 16:26
I'm sure you'll get it though
Christopher McCormack
@cmccormack
May 11 2018 16:26
all of my testing and on glitch the files were all hosted from the root (votery.glitch.me/, localhost:3000/, etc...) but on my server it's hosted from ...com/votery/, and it breaks all my routes
Know how to fix it? :D
Jason Luboff
@JLuboff
May 11 2018 16:26
Whats the underlying web service (IIS, Apache, etc?)
Christopher McCormack
@cmccormack
May 11 2018 16:27
nginx
Jason Luboff
@JLuboff
May 11 2018 16:27
Oh... never worked with that.
Christopher McCormack
@cmccormack
May 11 2018 16:27
I got far enough to get the static files hosted but everything else still hits my default route because I have no routes for /votery/blah
I think the nginx stuff is all working ok
Jason Luboff
@JLuboff
May 11 2018 16:28
Did you try hitting it with a hammer?
Christopher McCormack
@cmccormack
May 11 2018 16:28
a few times
Jason Luboff
@JLuboff
May 11 2018 16:30
Thats my only fix. Fixed my imaging server no problem!
Christopher McCormack
@cmccormack
May 11 2018 16:31
haha
Jason Luboff
@JLuboff
May 11 2018 16:32
Did you get your template(s) done?
Christopher McCormack
@cmccormack
May 11 2018 16:33
I did! Yesterday was actually pretty boring because that got finished and I wasn't sure what to start haha
Jason Luboff
@JLuboff
May 11 2018 16:34
Hah.. sounds like yesterday was successful for both of us, my image finally deployed so I'm finally getting these dozen computer sgoing haha
Christopher McCormack
@cmccormack
May 11 2018 16:34
that's awesome - was it a simple fix?
Jason Luboff
@JLuboff
May 11 2018 16:35
No. It took me two weeks :joy: The finally fix was apparently ADK (Windows Assesment and deployment kit) has to be at or above the build of Windows 10 so I had to uninstall and then reinstall that which finally fixed things
but there were a lot of little things that ultimately got it going
Christopher McCormack
@cmccormack
May 11 2018 16:35
jeeze sounds like a pain
Jason Luboff
@JLuboff
May 11 2018 16:36
It was. But now its running which is great and I know what I need to do for the future (As long as I remember...I sohuld probably take notes)
Christopher McCormack
@cmccormack
May 11 2018 16:42
yeah man before you forget
just don't use a microsoft tool to do it :p
Jason Luboff
@JLuboff
May 11 2018 16:44
:P
Where my emoji?!
Christopher McCormack
@cmccormack
May 11 2018 16:50
mine aren't working either
Jason Luboff
@JLuboff
May 11 2018 16:50
Sad..
Kudzu
@czhower
May 11 2018 17:07
Mine work :)
But they are from 1960 text ones
Timi Hraš
@timihras
May 11 2018 17:09
Hi. Can I post a question here, that is not about challenges? :)

I have two arrays. I would like to get a value from the second array based on the information from the first array. I get undefined always. :(

export default (pets, customers, petId) => {

  const pet = pets.find(({ id }) => id === petId);
  const owner = customers.find(({ id }) => id === pet.owner);

  return {
    ...pet,
    ownerName: `${owner.firstName} ${owner.lastName}`
  };
}

What is the correct way to chain this two calls?

Jason Luboff
@JLuboff
May 11 2018 17:12
@timihras Is pets a database collection?
Timi Hraš
@timihras
May 11 2018 17:13
@JLuboff It is an array of objects..
Kudzu
@czhower
May 11 2018 17:13
const pet = pets.find(({ id }) => id === petId);
Why the extra braces around Iid?
Jason Luboff
@JLuboff
May 11 2018 17:13
Ah. Guess I didn't realize there was a find array method
might help to show us your data structures
Kudzu
@czhower
May 11 2018 17:14
and where is id scoped? is it some global?
it should be just:
 const pet = pets.find( (id) => id === petId);
when you add {} you create a new object with id as a prop instead of it being an arg.
Christopher McCormack
@cmccormack
May 11 2018 17:17
@czhower that's destructuring, isolates just id from the pet object
Kudzu
@czhower
May 11 2018 17:17
I dont see the use of it in his case though.
The {} appear unnecessary.
Christopher McCormack
@cmccormack
May 11 2018 17:17
that's how destructuring works
if you have three properties in an object and need only one, you can use braces like that to grab only what you need
Timi Hraš
@timihras
May 11 2018 17:18
@czhower as @cmccormack said. I could write
const pet = pets.find((pet) => pet.id === petId)
Kudzu
@czhower
May 11 2018 17:19
I think thats clearer. I know about deconstruction, but in such cases I think it just creates more confusion. Explicity is usually better and I dont see any added benefit in this case not to explicity use pet.id
and if expadned in future, then you have to add each one to the arg list rather than just have them all avail from the get go wtih pet
Christopher McCormack
@cmccormack
May 11 2018 17:20
it's actually really useful in practice, I use it all the time
Kudzu
@czhower
May 11 2018 17:20
It can be yes, but I just dont think it is in this situation. I think its more counterproductive in this situation due to less readability.
without adding any benefit in this case that I can see.
Christopher McCormack
@cmccormack
May 11 2018 17:21
only if you don't use destructuring :)
Kudzu
@czhower
May 11 2018 17:21
Im talking about this specific case, not in general.
its like for loops, sure they still have thier place, but 95% of the time for of is a better choice.
Brad
@bradtaniguchi
May 11 2018 17:24
ew destructuring :/ its like normal programming but backwards :P
Christopher McCormack
@cmccormack
May 11 2018 17:29
@timihras can you provide an example call? maybe there's something wrong in what you pass to the function
Timi Hraš
@timihras
May 11 2018 17:46

@cmccormack I am actually trying to use this in React. Data is something like this:

pets = [
  {id: 1, name: 'One', owner: 'X17'},
  {id: 2, name: 'Two', owner: 'X18'},
  {id: 3, name: 'Three', owner: 'X18'}
];

customers = [
  {id: 'X17', fistName: 'Jen', lastName: 'Gray'},
  {id: 'X18', fistName: 'Tim', lastName: 'Brown'}
]

and selector is

petSelector(state.pets, state.customers, props.match.params.id)
Now I am trying to get to the right owner..
const pet = pets.find(({ id }) => id === petId);
const owner = customers.find(({ id }) => id === pet.owner);
Christopher McCormack
@cmccormack
May 11 2018 17:47
are you getting a pet if you log pet between the two find?
also log your props.match, did you mean to use query not params?
Kudzu
@czhower
May 11 2018 17:49
The examples here are pretty much exactly what you want to do:
dont even need the ()
Christopher McCormack
@cmccormack
May 11 2018 17:49
hmm query may be for node/express
Kudzu
@czhower
May 11 2018 17:49
blob
Christopher McCormack
@cmccormack
May 11 2018 17:50
his find callback looks fine to me, more likely he's not getting a value with props.match.params.id
Timi Hraš
@timihras
May 11 2018 17:51
It looks like, I have to find a mistake one step back. Thanks. Const pet is not getting defined.
Jason Luboff
@JLuboff
May 11 2018 17:55
@cmccormack Thanks
CamperBot
@camperbot
May 11 2018 17:55
jluboff sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1448 | @cmccormack |http://www.freecodecamp.org/cmccormack
Christopher McCormack
@cmccormack
May 11 2018 17:56
@timihras :thumbsup:
@JLuboff np
Michael S
@Ohshi60
May 11 2018 18:12
{ current_page: 1,
  limit: 20,
  next_page: 1,
  previous_page: 1,
  results: 
   [ { id: 'K6UvXD5Mexc',
       joke: 'Which side of the chicken has more feathers? The outside.' },
     { id: 'It4Etrjiqrc',
       joke: 'Why does a chicken coop only have two doors? Because if it had four doors it would be a chicken sedan.' },
     { id: 'NJJ6wH6hNCd',
       joke: 'What do you get when you cross a chicken with a skunk? A fowl smell!' },
     { id: 'dpGtcxPKuzd',
       joke: 'Do you know where you can get chicken broth in bulk? The stock market.' } ],
  search_term: 'chicken',
  status: 200,
  total_jokes: 4,
  total_pages: 1 }
In this JSON is there a way to access specific results without using their id? ie. results[0]?
Kudzu
@czhower
May 11 2018 18:21
access it from where?
Jason Luboff
@JLuboff
May 11 2018 18:21
@Ohshi60 results[0] would give you the entire object
Kudzu
@czhower
May 11 2018 18:21
normally you read JSON into an array, but JSON if there is only one object doesnt return an array
Jason Luboff
@JLuboff
May 11 2018 18:21
You than need to specify either id or joke results[0].joke
Michael S
@Ohshi60
May 11 2018 18:22
i am supposed to return a random joke
Kudzu
@czhower
May 11 2018 18:22
in my code when I read JSON I check to see if its an array or not, and if not then I makea single item array so all code that consumes it always finds and array.
Michael S
@Ohshi60
May 11 2018 18:22
tried testing it with results[0].joke
Kudzu
@czhower
May 11 2018 18:22
in that case, yes results[0] will give you the first one
Michael S
@Ohshi60
May 11 2018 18:23
request.get(options, function(err, response, body){
    if (err) throw err
    console.log('Statuscode: '+response.statusCode)
    switch (body.total_jokes) {
      case  0:
        console.log('No jokes were found using the search term: ' + term)
        break;
      case 1:
        console.log(body.results[0].joke)
        break;
      case body.total_jokes>1:
        console.log(body.results[0].joke)
        break;
      default:
        console.log('Error')

    }
    console.log(body)
  }
)
Kudzu
@czhower
May 11 2018 18:23
but you have no guarantee [0] is id: 1 unless you tell the server to send it sorted, but in your case that probably doesnt matter
Michael S
@Ohshi60
May 11 2018 18:23
I get default case on the chicken query
Jason Luboff
@JLuboff
May 11 2018 18:24
Have you checked to make sure body has data?
Michael S
@Ohshi60
May 11 2018 18:24
yes that json i posted earlier is it
Kudzu
@czhower
May 11 2018 18:24
why do you even need a case statement? You either have jokes or you dont, and in JS yuou can check undefined for result as well for array indexing.
Michael S
@Ohshi60
May 11 2018 18:24
body.total_jokes also returns 4
if i get more than 1 joke on return im supposed to show a random joke and then save it to a file
guess i coulda done if elseif else but thought case would be easier
Kudzu
@czhower
May 11 2018 18:26
My opinion would be that an if is better. x = Count > 1 ? random : 0
then check undefined after you get the result.
Michael S
@Ohshi60
May 11 2018 18:28
okay just checked results[0].joke and it works fine. Must be the switch case >:(
Kudzu
@czhower
May 11 2018 18:36
suggest changing to an if and simplifying. If your cases 3 or fewer, an if is usually better.
Michael S
@Ohshi60
May 11 2018 18:39
yes i changed it and it works thank you :)
i was thinking 3 cases and error handling, thats 4! :D
Kudzu
@czhower
May 11 2018 18:46
with better logic, you wont even need 3 cases. :)
it can be split into more discrete steps of logic
Jason Luboff
@JLuboff
May 11 2018 18:48
God.. I just made a disgusting piece of code all to get data in a structure I want..but.. I know it can be simplier.. currently has 4 loops (5 if including indexOf) :neutral_face: )
Michael S
@Ohshi60
May 11 2018 18:48
are you thinking about terniary expressions?
Kudzu
@czhower
May 11 2018 18:49
ternary is one of many possibilities.
Michael S
@Ohshi60
May 11 2018 18:49
saw your code, but my brain is still recovering from callback functions
Kudzu
@czhower
May 11 2018 18:49
basically id do it like this:

1) index = random ( 0 to count - 1) - if only one item, it will always return 0. No need to check.

2) joke = array[index]

3) If !joke (ie undefined) then we dont have any, else display the joke

step 1 - either branch out there so you dont ahve to check in 3, or return 0 so step 2 will return undefined. first approach better.
ie step 1 is if count =0 do show no joke, else do step 1&2 from above and forget #3
Kudzu
@czhower
May 11 2018 18:55

clearer:

if count = 0
show "no jokes"
else
index = random (0 to count -1)
joke = jokes[index]

Michael S
@Ohshi60
May 11 2018 18:56
arh yes i see, but if i do it like that i still cant do an else to output error
Moisés Man
@moigithub
May 11 2018 18:56
switch case works using ===
soo if u evaluate a numeric expression switch (body.total_jokes) {
cases values should be also numeric.. case 1:
soo this wont work case body.total_jokes>1: <-- boolean
Kudzu
@czhower
May 11 2018 18:58
do you need to check for an errorr? count will always be 0 or greater no?
Michael S
@Ohshi60
May 11 2018 18:59
@moigithub i see, thanks for clearing that up. In that case i could say case 2: since i can then run my code for cases 2 or greater
CamperBot
@camperbot
May 11 2018 18:59
ohshi60 sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3747 | @moigithub |http://www.freecodecamp.org/moigithub
Moisés Man
@moigithub
May 11 2018 18:59
if u wanna write boolean expression on ur cases.. u can use something like
switch (true) {  // boolean
case body.total_jokes === 0:   // boolean
    /// sometihng
   break;
case body.total_jokes>1:    // boolean
   // another thing
  break;
}
Michael S
@Ohshi60
May 11 2018 18:59
or done it that way, much simpler lol :D
@czhower hey when i think about it i handle error in the callback anyway so ure probably right :)
Kudzu
@czhower
May 11 2018 19:00
a case is still inadvisable for only 2 conditions. an if would be preferable.
Michael S
@Ohshi60
May 11 2018 19:01
yes i didnt think about combining = 1 and greater than one into one though
Kudzu
@czhower
May 11 2018 19:02
you can do that by calling random for all > 0
Michael S
@Ohshi60
May 11 2018 19:03
i know, im doing it, just saying i didnt think about it and i was counting error handling as well so a switch made sense to me at the time
but thanks again for clearing up why my switch didnt work, TIL
Tom
@moT01
May 11 2018 22:16
anyone a chartjs master?
Christopher McCormack
@cmccormack
May 11 2018 22:17
@moT01 I once made a graph!
Tom
@moT01
May 11 2018 22:17
ha
well, i have a data set that will show a percent from average e.g [-50, 0, 50] - 0 being average and the others being +/- 50%, but i want the ticks to say other things - i want the 0 to say "average"
probly just have dug deep enough
Christopher McCormack
@cmccormack
May 11 2018 22:21
yeah there's usually good tick info if you dig
took me a long time in D3 to do it that's for sure
Tom
@moT01
May 11 2018 22:21
havent*
yea, chartjs is a little easier
but less customizable
Christopher McCormack
@cmccormack
May 11 2018 22:23
yeah
Formidable's Victory is kind of cool too
Jason Luboff
@JLuboff
May 11 2018 22:27
Oh I used chartjs before!
Tom
@moT01
May 11 2018 22:27
never heard of it
on the vote app? @JLuboff
Jason Luboff
@JLuboff
May 11 2018 22:29
Mmm...I think I used it for both voting app and stock app
Michael S
@Ohshi60
May 11 2018 23:34
so i finally wrapped my head around callbacks, and in come promises and make me question existence :(