I want to calculate total sales - javascript

This is my data and I'd like to calculate total sales.
let order = [
{ id: 'A441', sales: [10, 12, 13, 10, 16, 22, 30] },
{ id: 'B234', sales: [2, 4, 3, 4, 2, 6, 8, 10] },
{ id: 'A617', sales: [5, 5, 5, 5, 5] },
{ id: 'C229', sales: [9, 7, 6, 8, 8, 10, 9, 3, 4, 5, 6] },
{ id: 'D412', sales: [25, 25, 23, 21] },
{ id: 'A054', sales: [2, 2, 3, 1, 5, 6, 7, 11, 2] },
{ id: 'B955', sales: [1, 1, 1, 1, 1, 1] },
{ id: 'M341', sales: [4, 5, 4, 5, 4] },
{ id: 'H103', sales: [3, 2, 2, 3, 1, 1] },
{ id: 'B199', sales: [6, 5, 4] },
{ id: 'D388', sales: [7, 8, 9, 8, 4, 4, 4, 3, 2, 1] }
];

If you want the total by sale type:
let orders = [
{ id: 'A441', sales: [10, 12, 13, 10, 16, 22, 30] },
{ id: 'B234', sales: [2, 4, 3, 4, 2, 6, 8, 10] },
{ id: 'A617', sales: [5, 5, 5, 5, 5] },
{ id: 'C229', sales: [9, 7, 6, 8, 8, 10, 9, 3, 4, 5, 6] },
{ id: 'D412', sales: [25, 25, 23, 21] },
{ id: 'A054', sales: [2, 2, 3, 1, 5, 6, 7, 11, 2] },
{ id: 'B955', sales: [1, 1, 1, 1, 1, 1] },
{ id: 'M341', sales: [4, 5, 4, 5, 4] },
{ id: 'H103', sales: [3, 2, 2, 3, 1, 1] },
{ id: 'B199', sales: [6, 5, 4] },
{ id: 'D388', sales: [7, 8, 9, 8, 4, 4, 4, 3, 2, 1] }
];
function getTotalSales(orders){
return orders.map(order=> ({...order, totalSales: order.sales.reduce((acc, cur)=> acc+=cur, 0)}))
}
console.log(getTotalSales(orders))
If you want the total of all sales:
let orders = [
{ id: 'A441', sales: [10, 12, 13, 10, 16, 22, 30] },
{ id: 'B234', sales: [2, 4, 3, 4, 2, 6, 8, 10] },
{ id: 'A617', sales: [5, 5, 5, 5, 5] },
{ id: 'C229', sales: [9, 7, 6, 8, 8, 10, 9, 3, 4, 5, 6] },
{ id: 'D412', sales: [25, 25, 23, 21] },
{ id: 'A054', sales: [2, 2, 3, 1, 5, 6, 7, 11, 2] },
{ id: 'B955', sales: [1, 1, 1, 1, 1, 1] },
{ id: 'M341', sales: [4, 5, 4, 5, 4] },
{ id: 'H103', sales: [3, 2, 2, 3, 1, 1] },
{ id: 'B199', sales: [6, 5, 4] },
{ id: 'D388', sales: [7, 8, 9, 8, 4, 4, 4, 3, 2, 1] }
];
function getTotalSales(orders){
return orders.map(({sales})=> sales).flat().reduce((acc, cur)=> acc+=cur, 0)
}
console.log(getTotalSales(orders))

Related

reverse the numbers of an seperated array of arrays using .reverse() JS

I want to reverse the numbers of the different arrays in "batch" and return it as new Array.
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
This is my code so far:
const validateCred = cardID =>{
let arr = cardID
return arr.slice().reverse(arr.reverse())
}
console.log(validateCred(batch))
but it always reverse just the individual arrays, not the numbers inside.
Pls explain why yours will work and what's wrong with mine and if batch would be a nested array or not.
Thx <3
This function would solve your issue.
const validateCred = cardIDs => {
return cardIDs.map(cardID => cardID.slice().reverse());
}
You are currently only reversing the batch array. Not the arrays inside of it.
You are passing the wrong data type to the validateCred function.
Here you are passing an array of array of numbers to a function that expects an array of numbers:
console.log(validateCred(batch))
To fix it, take that and feed it to Array.map:
console.log(batch.map(cardID => validateCred(cardID)));
Snippet:
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
const validateCred = cardID => {
let arr = cardID
return arr.slice().reverse(arr.reverse())
}
console.log(batch.map(cardID => validateCred(cardID)));

How to calculate the averages of the arrays of multiple arrays [duplicate]

This question already has answers here:
Average of bidimensional array's columns with array.map()
(6 answers)
How to compute the sum and average of elements in an array? [duplicate]
(35 answers)
Loop through an array in JavaScript
(46 answers)
Closed 4 months ago.
In JavaScript I generate an x number of arrays, all consisting 57 numbers.
I want to calculate the average of each single number in the array as a result in one array with the averages i.e.:
array1[0] + array2[0] + array3[0] .... / number of arrays = average of [0]
array1[1] + array2[1] + array3[1] .... / number of arrays = average of [1]
array1[2] + array2[2] + array3[2] .... / number of arrays = average of [2]
This is an example of a generated array of arrays:
(5) [Array(57), Array(57), Array(57), Array(57), Array(57)]
0
:
(57) [4, 3, 3, 4, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 4, 3, 5, 5, 4, 4, 4, 4, 3, 4, 3, 4, 3, 4, 4, 4, 4, 3, 4, 3, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 5, 3, 5]
1
:
(57) [1, 3, 1, 2, 1, 3, 2, 2, 3, 1, 2, 1, 3, 2, 3, 1, 4, 4, 4, 4, 4, 3, 1, 1, 4, 3, 3, 2, 2, 2, 4, 3, 3, 3, 3, 2, 3, 3, 3, 1, 2, 1, 1, 2, 2, 4, 1, 4, 1, 1, 3, 4, 3, 2, 1, 2, 4]
2
:
(57) [1, 3, 2, 4, 4, 4, 3, 4, 5, 4, 2, 4, 2, 3, 3, 4, 2, 1, 1, 2, 2, 1, 2, 3, 4, 1, 1, 1, 3, 3, 3, 3, 2, 2, 2, 4, 4, 1, 2, 3, 2, 2, 4, 3, 4, 2, 4, 4, 2, 2, 1, 2, 3, 1, 2, 1, 1]
3
:
(57) [2, 4, 4, 3, 2, 3, 1, 4, 3, 3, 1, 2, 1, 1, 3, 4, 4, 2, 3, 2, 2, 1, 2, 2, 4, 1, 2, 1, 1, 4, 2, 3, 3, 3, 1, 4, 1, 4, 1, 4, 4, 4, 1, 1, 3, 3, 3, 4, 1, 3, 2, 1, 4, 1, 1, 2, 4]
4
:
(57) [1, 1, 3, 1, 1, 2, 4, 4, 5, 1, 2, 1, 2, 2, 3, 4, 4, 2, 1, 3, 1, 2, 4, 3, 4, 4, 4, 4, 1, 3, 1, 2, 2, 4, 1, 4, 1, 3, 3, 4, 1, 3, 4, 2, 4, 1, 4, 3, 4, 1, 2, 4, 4, 1, 1, 4, 4]
length
:
5
[[Prototype]]
:
Array(0)
Can anyone give me an example whereby I can build this solution properly?
assuming rawArrays is your generated array of arrays of numbers.
const amountOfArrays = 2, amountOfNumbersInArray = 3;
const rawArrays = [
[1, 2, 3],
[4, 5, 6],
];
const averagesOnArrayIndex = rawArrays[0].map((_, id) => {
const sumOfElementsOnThisId = rawArrays.map((array) => array[id]).reduce((acc, cur) => acc += cur, 0);
const averageOnThisId = sumOfElementsOnThisId / amountOfArrays;
return averageOnThisId;
});

How can I keep the return value of a function as a nested array?

I'm coding a program using the Luhn Algorithm to check for valid and invalid credit card numbers. These numbers are single arrays but you can batch them inside a nested array. What I'm trying to do is reverse their order, then save them inside an array called reversedNumbers (this is a nested array as well), after that I've tried to do the following but the result is not what I wanted since the return value is a single array not arrays nested inside an array.
CODE BELOW:
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
// Add your functions below:
const validateCred = arry => {
let reversedNumbers = [];
let newArray = [];
for (let i = 0; i < arry.length; i++) {
reversedNumbers.push(arry[i].slice().reverse());
}
//return console.log(reversedNumbers);
for (let j = 0; j < reversedNumbers.length; j++) {
for (let k = 0; k < reversedNumbers[j].length; k++) {
if (k % 2 !== 0) {
if ((reversedNumbers[j][k] * 2) > 9) {
newArray.push((reversedNumbers[j][k] * 2) - 9);
} else {
newArray.push(reversedNumbers[j][k]);
}
} else {
newArray.push(reversedNumbers[j][k]);
}
}
}
return console.log(newArray);
}
validateCred(batch);
RESULT:
You're just pushing everything onto newArray, you're not creating any nested arrays. You need to create an array each time through the outer loop, and push that onto newArray.
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
// Add your functions below:
const validateCred = arry => {
let reversedNumbers = [];
let newArray = [];
for (let i = 0; i < arry.length; i++) {
reversedNumbers.push(arry[i].slice().reverse());
}
//return console.log(reversedNumbers);
for (let j = 0; j < reversedNumbers.length; j++) {
let newSubArray = [];
for (let k = 0; k < reversedNumbers[j].length; k++) {
if (k % 2 !== 0) {
if ((reversedNumbers[j][k] * 2) > 9) {
newSubArray.push((reversedNumbers[j][k] * 2) - 9);
} else {
newSubArray.push(reversedNumbers[j][k]);
}
} else {
newSubArray.push(reversedNumbers[j][k]);
}
}
newArray.push(newSubArray);
}
return newArray;
}
console.log(validateCred(batch));
You can also do it using map().
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];
// Add your functions below:
const validateCred = arry => {
let reversedNumbers = arry.map(a => a.slice().reverse());
let newArray = reversedNumbers.map(numbers => numbers.map((num, k) => {
if (k % 2 !== 0) {
if ((num * 2) > 9) {
return (num * 2) - 9;
} else {
return num;
}
} else {
return num;
}
}))
return newArray;
}
console.log(validateCred(batch));

Having trouble accessing the nested values of my object. JavaScript

So I am trying to create a function that will list all the stats of a specific player in below object. I am simply trying to return the nested object associated with the playerName itself. I'm guessing map isn't working for me here. Instead I am returning an array of the individual letters of the name "Jeff Adrien". Can someone help me understand where I am going wrong?
//Code in question
function playerStats(playerName) {
let specificPlayer = allPlayers().players
if (playerName === specificPlayer) {
return specificPlayer
}
const stats = Object.values(playerName).map((nums) => {
return [nums]
})
return stats.flat()
}
console.log(playerStats('Jeff Adrien'))
// details
function allPlayers() {
const everyPlayer = Object.assign(homePlayers, awayPlayers)
return everyPlayer
}
const gameObject = () => {
return {
home: {
teamName: 'Brooklyn Nets',
colors: ['black', 'white'],
players: {
'Alan Anderson': {
number: 0,
shoe: 16,
points: 22,
rebounds: 12,
assists: 12,
steals: 3,
blocks: 1,
slamDunks: 1,
},
'Reggie Evans': {
number: 30,
shoe: 14,
points: 12,
rebounds: 12,
assists: 12,
steals: 12,
blocks: 12,
slamDunks: 7,
},
'Brook Lopez': {
number: 11,
shoe: 17,
points: 17,
rebounds: 19,
assists: 10,
steals: 3,
blocks: 1,
slamDunks: 15,
},
'Mason Plumlee': {
number: 1,
shoe: 19,
points: 26,
rebounds: 12,
assists: 6,
steals: 3,
blocks: 8,
slamDunks: 5,
},
'Jason Terry': {
number: 31,
shoe: 15,
points: 19,
rebounds: 2,
assists: 2,
steals: 4,
blocks: 11,
slamDunks: 1,
},
},
},
away: {
teamName: 'Charlotte Hornets',
colors: ['turquoise', 'purple'],
players: {
'Jeff Adrien': {
number: 4,
shoe: 18,
points: 10,
rebounds: 1,
assists: 1,
steals: 2,
blocks: 7,
slamDunks: 2,
},
'Bismak Biyombo': {
number: 0,
shoe: 16,
points: 12,
rebounds: 4,
assists: 7,
steals: 7,
blocks: 15,
slamDunks: 10,
},
'DeSagna Diop': {
number: 4,
shoe: 14,
points: 24,
rebounds: 12,
assists: 12,
steals: 4,
blocks: 5,
slamDunks: 5,
},
'Ben Gordon': {
number: 8,
shoe: 15,
points: 33,
rebounds: 3,
assists: 2,
steals: 1,
blocks: 1,
slamDunks: 0,
},
'Brendan Haywood': {
number: 33,
shoe: 15,
points: 6,
rebounds: 12,
assists: 12,
steals: 22,
blocks: 5,
slamDunks: 12,
},
},
},
}
}
wow I was trying to work with an array and turn it into an object somehow.. it just clicked for me
function playerStats(playerName) {
for (const player in allPlayers()) {
if (player === playerName) {
return allPlayers()[player]
}
}
}

Returning Array names in array of arrays

Is there a way to call the names of the arrays in an array of arrays? I am applying a Luhn algorithm to a set of credit card numbers in an array of arrays I would like output that says, 'Array xxx is true/false.' I have created a workaround below by creating an array of "names" but can the batch array names be accessed directly?
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5,5,3,5,7,6,6,7,6,8,7,5,1,4,3,9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]
const arrNames = ['valid1', 'valid2', 'valid3', 'valid4', 'valid5', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5', 'mystery1', 'mystery2', 'mystery3', 'mystery4', 'mystery5']
const batchL = batch.length;
function ValidateCred(CC) {
if (CC.length > 19) return (false);
sum = 0;
mul = 1;
l = CC.length-1;
for (i = 0; i < l+1; i++) {
digit = CC[l - i];
tproduct = digit * mul;
if (tproduct >= 10) sum += (tproduct % 10) + 1;
else sum += tproduct;
if (mul == 1) mul++;
else mul--;
};
if ((sum % 10) == 0) return (true);
else return (false);
};
for (b = 0; b < batchL; b++) {
console.log('Card', arrNames[b],'is: ', ValidateCred(batch[b]));
};
Probably the best approach to this problem is to use an object instead of an array:
const validCCNumbers = {
valid1: [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8],
valid2: [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9],
valid3: [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6],
valid4: [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5],
valid5: [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]
}
If you need to access the names of each array you can do it with Object.keys(validCCNumbers)
You can shorten the whole operation considerably:
const all= {valid1:["4539677908016808", "5535766768751439", "371612019985236", "6011144340682905", "4539404967869666"],
invalid1:["4532778771091795", "5795593392134643", "375796084459914", "6011127961777935", "5382019772883854"],
mystery1:["344801968305414", "5466100861620239", "6011377020962656203", "4929877169217093", "4913540463072523"]};
Object.entries(all).forEach(([grname,arr])=>{
console.log(grname);
console.log(arr.map(cc=>cc+": "+ValidateCred(cc)));
});
function ValidateCred(ccstr) {
if (ccstr.length > 19) return (false);
let CC=ccstr.split(""), digit, tproduct,
sum = 0, mul = 1, l = CC.length-1;
for (i = 0; i < l+1; i++) {
digit = CC[l - i];
tproduct = digit * mul;
if (tproduct >= 10) sum += (tproduct % 10) + 1;
else sum += tproduct;
if (mul == 1) mul++;
else mul--;
};
return sum % 10 == 0
};

Categories