Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Dec 21 2018 17:11
    @MadLittleMods banned @OmarAlmaqtri
  • Jun 05 2018 23:13
    @MadLittleMods banned @LWSS
  • May 25 2018 16:50
    @MadLittleMods banned @TheRedstoneTaco
Mahi
@arbitrarymahi
I have written this code and it gets the response but due to some reason it is logging to the console twice.
Stephen James
@sjames1958gm
Is it hitting both console.log?
dimtabu
@taburetkin
if it logs twice then it means that onreadystatechange called two times
I just put my Node.js authentication boilerplate on Github. Check it out.
ptmhpp
@ptmhpp

Hi Guys…..
I have a javascript file and in that there are some functions defined in it.
How can i get all the function name defined in that particular js file in an array of string

for example js file looks something like this

function getabc1() {
//block of code
}
function getabc2() {
//block of code
}
function getabc3() {
//block of code
}

I need all the 3 functions name defined in this particular js file.
Only name of function
It will be great help if someone can help me on this.

dimtabu
@taburetkin
short: process the js file in some other runtme, read it as text and find something by pattern and store it somehow somewhere.
But i believe you are doing something certain and your question is partial.
I believe that there is more easy/better ways exists to achieve what you really want
alienroom
@alienroom
any 1 here doing stream in programming javascript? i would love to watch and learn i am level 5 of 10 in javascript thank you community.
Braulio
@bab2683
@lilalienx what do you mean you are level 5 of 10 in javascript?
alienroom
@alienroom
@bab2683 imagine 10 is top 5 is middle
Braulio
@bab2683
@lilalienx yeah but how can you tell what level are you? is there a scale or something?
EuphoricPenguin
@EuphoricPenguin
General syntax question: Is setting a variable using var foo = "bar"; and then later in the code reassigning the value without var: foo = "new value"; treated the same as using var for each value reassign?
EuphoricPenguin
@EuphoricPenguin
@bab2683 @lilalienx I don't think you could ever really measure where you are in JS. There's so many components: modern syntax usage, coding concept understanding, code structure and implementation, use of code modules strategically, etc...
You can get an idea in your head, but I always find more to learn. JS seems easy at first, but you can always learn more.
If you're still using for loops with arrays, for example, there's other options. for/in and for/of, plus forEach.
Braulio
@bab2683
@EuphoricPenguin that is what I was trying to say, you can't really declare you are "some level of javascript" because there is always something new to learn
dimtabu
@taburetkin
actually, its true for anything in the world
EuphoricPenguin
@EuphoricPenguin
General syntax question: Is setting a variable using var foo = "bar"; and then later in the code reassigning the value without var: foo = "new value"; treated the same as using var for each value reassign?
Still, about this ^
Is setting a variable as a property after declaring it by name to be a variable syntactically the same as using var for each value reassign?
dimtabu
@taburetkin
EuphoricPenguin
@EuphoricPenguin
@taburetkin I know how to use it, all I'm asking is if var is only used the first time for reassigning values.
dimtabu
@taburetkin
actually, links i give you completely answers your question, but to get the answer you have to read
EuphoricPenguin
@EuphoricPenguin
It looked like just scope.
EuphoricPenguin
@EuphoricPenguin
@taburetkin Saw an article here, so you don't use var to reassign. I never did, but for some reason I was worried lol.
EuphoricPenguin
@EuphoricPenguin
Oh, just saw this and was curious: What is the "?" operator? Is it shorthand, I've only seen it used a couple of times.
Tried to find it on MDN and Google, no luck.
Nvm, found it.
Assigns an optional value based on condition.
Nadia
@nadiaschutz
// how would you improve this code in node.js? any suggestions? thanks
const axios = require('axios')
const chalk = require('chalk');
const url = "http://api.genderize.io"
const names = [];
let params;

for (let j = 2; j < process.argv.length; j++) {
    if (isNaN(Number(process.argv[j]))) {
        names.push(process.argv[j]);
    }
}


if (names.length === 0) {
    console.log('Please provide names');
} else if (names.length === 1) {
    params = "?name=" + names;
} else {
    const result = [];
    for (let j = 0; j < names.length; j++) {
        (j === 0) ? params = "/?name[]=" + names[j] : result.push("&name[]=" + names[j]);
    };
    result.forEach(element => {
        params += element;
    });
}


axios({
    method: 'get',
    url: url + params,
    headers: {
        'Accept': 'application/json'
    },
}).then(res => {
    checkGender(res.data);
}).catch(err => {
    const log = chalk.red(err) // we set the color red here for errors.
    console.log(log)
})

const checkGender = (data) => {
    const result = [];
    females = [];
    males = [];
    data.forEach(name => {
        name.gender === 'female' ? females.push(name) : males.push(name)
    });
    result.push(females.sort(function (a, b) {
        return b.probability - a.probability;
    }));
    result.push(males.sort(function (a, b) {
        return a.probability - b.probability;
    }));

    console.log(result);
}
Nadia
@nadiaschutz
➜ node gender.js tom jack maria anna Alexis
[ [ { name: 'anna', gender: 'female', probability: 1, count: 4755 },
    { name: 'maria',
      gender: 'female',
      probability: 0.99,
      count: 8402 } ],
  [ { name: 'Alexis', gender: 'male', probability: 0.52, count: 1224 },
    { name: 'jack', gender: 'male', probability: 0.99, count: 1993 },
    { name: 'tom', gender: 'male', probability: 1, count: 3736 } ] ]
dimtabu
@taburetkin
@nadiaschutz
const checkGender = (data) => {
    let genders = { 
        male: [1, (a,b) => a.probability - b.probability], 
        female: [-1, (a,b) => b.probability - a.probability] 
    };
    data.sort((a,b) => {
       if (a.gender == b.gender) {
          return genders[a.gender][1](a,b);
       }
       return genders[a.gender][0] - genders[b.gender][0];
    });
    console.log(data);
}
Nadia
@nadiaschutz
const checkGender = (data) => {
    let females = (data.filter(item => { return item.gender === 'female' })).sort((a, b) => {
        return b.probability - a.probability;
    });
    let males = (data.filter(item => { return item.gender === 'male' })).sort((a, b) => { return a.probability - b.probability });
    let result = [...females, ...males];

    console.log(result);
}
dimtabu
@taburetkin
i belive you can sort in one go, like in my example
without creating three additional arrays.
Nadia
@nadiaschutz
@taburetkin could you please explain what does each step do?
dimtabu
@taburetkin
sure
Nadia
@nadiaschutz
можешь на русском если удобнее
в крайнем случае на китайском
dimtabu
@taburetkin
))))
khinekhinemyatnoe
@babemyatnoe_gitlab
Hello there!:)
can someone help me?
i do need help with js codes.
i'm new and can i get a chance to get a reply?
khinekhinemyatnoe
@babemyatnoe_gitlab
i've downloaded this https://codepen.io/elmahdim/pen/tEeDn and i wanted to add their js function to my own design
nicfaust
@nicfaust
🙄 беда печаль
Abdul Basit
@Abdul___Says_twitter
@babemyatnoe_gitlab please ask, which problem are you facing ?
Richard Sithole
@Sliqric7053

Hi there

I have a 10x4 grid of checkboxes (call it x, y) - I would like to get the coordinates (x, y values) of the checkbox when a user selects one. What data structure can I use to pluck out those coordinates?

I've tried using a Map like so:

 onCheck(e) {
    const productMap = new Map();

    productMap.set('productId', e.path[2].id.split('-').pop());
    productMap.set('productLevelId', e.path[2].id.split('-').pop());
}

The checkboxes are generated dynamically, so I use a click event listener and pass in the checkbox element..

The backend expects from me, a response like this:

{
productId: 4,
productLevelId: 1
}

Any ideas how I can accomplish this?

dimtabu
@taburetkin
when you click on a checkbox then change event is emited
event.target in that case will point to your checkbox dom element