A general chat about JavaScript. Ask anything you like or share your best cat picture. Please check Google/StackOverflow for answers before asking for help. And remember, be nice! Don't ask to ask, just ask
// 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);
}
➜ 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 } ] ]
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);
}
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);
}
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?
<input type="checkbox" data-id="yourProductId" data-levelid="yourLevelId">
```
this.myForm.value.arr.forEach(form => {
this.filesToSend.forEach(element => {
element = element + '=' + form.password;
console.log(element);
});
});
console.log(this.myForm);
console.log(this.filesToSend);
}
```
Hi All I have run into a problem I am uanble to call values of Array from different JS file. See the attached code for your refernence
File1//
import {DataVal} from './DateValue';
DataVal.map((item) =>{
console.log ("Date: "+ item.date);
});
File2//
export const DataVal =
[
{
date:"07/08/2010" .........................................
......................................