These are chat archives for FreeCodeCamp/HelpJavaScript

17th
Jun 2018
Brad
@bradtaniguchi
Jun 17 2018 00:45
@koder3 Its using a regex, or a Regular Expression
https://en.wikipedia.org/wiki/Regular_expression - for more a theoretical approach
Simon Cordova
@gbsimon87
Jun 17 2018 00:48
Thanks @megantaylor
Ryan Williams
@Ryanwfile
Jun 17 2018 03:28
I am trying to create a multi level dropdown menu in React and most tutorials I've found are either not done in React or have incomplete instructions with imports from other code without explanations, are there any resources out there for this, seems like such a simple task and without react it takes 5 mins, any help is greatly appreciated.
Jack Shijin
@Jackshijin
Jun 17 2018 03:28
how to get the attribute of an array in an object
Megan Taylor
@megantaylor
Jun 17 2018 03:40
@Ryanwfile have you looked at this? https://engineering.tripping.com/building-an-recursive-nested-dropdown-component-in-react-b1c883e06ac4 seems useful, but i dont know what your app context is.
@Jackshijin need moar data pls
Ryan Williams
@Ryanwfile
Jun 17 2018 03:46
@megantaylor Thank you, I am just trying to do a simple nested dropdown in react for now, this is exactly what I'm looking for but it doesn't seem to work in codepen
Megan Taylor
@megantaylor
Jun 17 2018 03:54
@Ryanwfile also found this: https://react-component.github.io/menu/
that tripping example isn't a good one for codepen; needs data, props to be passed down to it
Ryan Williams
@Ryanwfile
Jun 17 2018 04:05
@megantaylor Yea most examples seem to import, export or use npm or modules, was hoping there was something simple out there since it's such a common task. This one https://blog.logrocket.com/building-a-custom-dropdown-menu-component-for-react-e94f02ced4a1 seems promising but it skips around a little and doesn't explain some steps or and doesn't seem to have an example with nested dropdowns.
Megan Taylor
@megantaylor
Jun 17 2018 04:11
honestly the idea of doing anything with react in codepen is very strange to me, but whatever. one thing you could do is build the whole thing out in plain old HTML and then slowly convert the pieces to react. Its like doing jsx backwards!
Ryan Williams
@Ryanwfile
Jun 17 2018 04:14
@megantaylor That was what I did initially and everything worked well except my menu's wouldn't close when another opened and everyone on here said I shouldn't be doing it with bootstrap/jquery so I tried just react. Thank you for all of the help
Megan Taylor
@megantaylor
Jun 17 2018 04:21
yea, bootstrap doesnt really support nested dropdowns, but you should absolutely be able to do it with jquery
Ryan Williams
@Ryanwfile
Jun 17 2018 04:24
@megantaylor Yea it's real easy too, took 1 min https://codepen.io/Ryanwfile/pen/bMwYam
Megan Taylor
@megantaylor
Jun 17 2018 04:27
Lovely! so what's wrong with that?
Ryan Williams
@Ryanwfile
Jun 17 2018 04:29
@megantaylor not much just need to create a function to toggle the dropdown to close when another is opened or the user clicks away from the dropdowns. Just the people kind enough to offer me help with that mostly said to not use jquery but I'm going to stick with it for now
Megan Taylor
@megantaylor
Jun 17 2018 04:32
:D all the cool kids are an the anti-jquery train now bc react is so secksy but i think 90% of the time making it work is better than being cool. i would only worry about it if this were for a big project where i was super worried about performance.
Ryan Williams
@Ryanwfile
Jun 17 2018 04:37
@megantaylor This is just for a real simple project to re acquaint myself with React so I'm not worried at all about performance just functionality. I wonder what happened to all of those FCC point thingies
seems they got rid of them since the FCC api isn't loading them for the Leaderboard project anymore, anyways thank you very much for all of the help
Megan Taylor
@megantaylor
Jun 17 2018 04:38
:thumbsup:
KingKongg
@KingKongg
Jun 17 2018 08:06

Hi, can someone help to clarify my doubt. I am doing one of the practice and I am confused.

Array.prototype.myFilter = function(callback)
{
var newArray = [];
// Add your code below this line

// Add your code above this line

return newArray;

};

For the function(callback) why is it when you want to use the function you use callback(), I thought the name of the function should be outside of the parentheses?

For example
function square(number){

return number*number
};

then when you call you use console.log(square(3)) for example.

Or is the callback is a function by itself?

Markus Kiili
@Masd925
Jun 17 2018 08:58
@KingKongg In this case, the function has no name. The function on the right hand side of = is called an function expression or an anonymous function. callback is the parameter of that function. That anonymous function is stored on property Array.prototype.myFilter. It is then used like [1 2 3].myFilter(...);
Because all arrays can use methods of Array.prototype along the prototype chain.
Phil
@mremotoo
Jun 17 2018 10:42
Why does my code doesn't return the numbers properly?
function newColor(){ var c1 = { r: Math.floor(Math.random()*255), g: Math.floor(Math.random()*255), b: Math.floor(Math.random()*255) }; var c2 = { r: Math.floor(Math.random()*255), g: Math.floor(Math.random()*255), b: Math.floor(Math.random()*255) }; c1.rgb = 'rgb('+c1.r+', '+c1.g+', '+c1.b+')'; c2.rgb = 'rgb('+c2.r+', '+c2.g+', '+c2.b+')'; return "linear-gradient(to right, '+c1.rgb+', '+c2.rgb+')";
Why does my code doesn't return the numbers properly?
function newColor(){
    var c1 = {
        r: Math.floor(Math.random()*255),
        g: Math.floor(Math.random()*255),
        b: Math.floor(Math.random()*255)
    };
    var c2 = {
        r: Math.floor(Math.random()*255),
        g: Math.floor(Math.random()*255),
        b: Math.floor(Math.random()*255)
    };
    c1.rgb = 'rgb('+c1.r+', '+c1.g+', '+c1.b+')';
    c2.rgb = 'rgb('+c2.r+', '+c2.g+', '+c2.b+')';
    return "linear-gradient(to right, '+c1.rgb+', '+c2.rgb+')";
Phil
@mremotoo
Jun 17 2018 10:48
This is ridiculous! lol I think I discovered by error. My return statement's quotes.
Jack Shijin
@Jackshijin
Jun 17 2018 14:06
c1.rgb
Is .rgb an arrtribute?
Nate Mallison
@NJM8
Jun 17 2018 14:48
That's not valid JS string templating. Use back ticks on the outside and dollar sign curly brackets for variables
hello ${myVar}
Wrapped in single back ticks
Jack Shijin
@Jackshijin
Jun 17 2018 15:29
hey guys
Shubham soni
@shubhamsoni2617
Jun 17 2018 17:49
let a=[1,2,3,4],
function get(C){
   let y=[];
  for(let i=0;i<C.length;i++){
     let x=C[i];
result=0;
    switch(x){
        case 3:
        result+=3;
        break;
         case 4:
        result+=4;
        break;

    }

  }
              return y+=result;

}
console.log(get(a));
I am expecting result to be 7 instead getting 4.
Thomas Tylek
@zippkidd
Jun 17 2018 17:55
You're setting result to 0 before case 4 @shubhamsoni2617
so your code gets to C[2] which is 3, and result is set to 0, and then you get case 3 and result += 3 makes result 3. Then you're expecting case 4 to run on the last iteration and add 4 to result and get you 7, but on that last iteration result is set to 0 before case 4 happens
so step 1: x = 1, result set to 0, switch statement matches nothing
step 2: x = 2, result is set to 0, switch statement matches nothing
step 3: x = 3, result is set to 0, switch statement matches case 3, 3 is added to result and result is set to 3
setp 4: x = 4, result is set to 0, switch statement matches case 4, 4 is added to result and result is set to 4
Shubham soni
@shubhamsoni2617
Jun 17 2018 18:14
@zippkidd Thanks, I have got the point but what possibly I could do to get 7?
Tiago Correia
@tiagocorreiaalmeida
Jun 17 2018 18:35
hello everyone
Thomas Tylek
@zippkidd
Jun 17 2018 19:37
@shubhamsoni2617 initialize the result variable outside of the for loop
Shubham soni
@shubhamsoni2617
Jun 17 2018 20:11

@zippkidd

let a=[1,2,3,4],
    b=[5,6,7,8];
function get(C){
   let y=0;
  let result =0;
  for(let i=0;i<C.length;i++){
     let x=C[i];

    switch(x){
        case 3:
        result+=3;
        break;
         case 4:
        result+=4;
        break;

    }
              return y+=result;


  }

}
console.log(get(a));

Getting 0

Tiago Correia
@tiagocorreiaalmeida
Jun 17 2018 21:21
cause on the first run of the loop
if it doesnt matches any of the statements on the switch it will return the y+result
Nico Mee
@nicoandmee
Jun 17 2018 21:21
https://gist.github.com/nicoandmee/145713df148fc1bbf98ebb216ddd40db Can someone help me? I want to pass in the locator strategy (xpath, id, name, css, etc) and use it in the By.[locatorStrategy] line. Can't figure it out. Tried string interpolation but it doesnt work.
Tiago Correia
@tiagocorreiaalmeida
Jun 17 2018 21:21
and when you do that the variables are at 0
Thomas Tylek
@zippkidd
Jun 17 2018 22:35
@shubhamsoni2617 as @tiagocorreiaalmeida said, it seems that this time you have your return y+= result; in the for loop but when that is reached the function ends entirely. Make sure to put the return statement below the for loop