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

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;
});

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)));

Trying to find the correct syntax for an when I need to take an if statement and check if the results are > 9 then add another if statement

I am doing a project for codecademy and am a little stuck on the syntax among other things ;). I am trying to reverse iterate an array and as I iterate to the left I need to double every other digit THEN check to see whether those numbers are greater than 9 after doubling them. THEN subtract the doubled number by 9. Any help would be much appreciated! This is my code so far
// 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:
function validateCred(arr) {
for (let i = arr.length - 2; i >= 0; i--)
if(i % 2 === 0) {
arr[i] = arr[i] * 2;
} else if (i % 2 === 1) {
arr[i]
}
}
arr[i] by itself doesn't do anything, so you can just leave out the else statemennt.
After doubling the number, use an if statement to check if it's higher than 9.
function validateCred(arr) {
for (let i = arr.length - 2; i >= 0; i--) {
if (i % 2 === 0) {
arr[i] *= 2;
if (arr[i] > 9) {
arr[i] -= 9;
}
}
}
}

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));

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
};

Trying to make a seat reservation system with javascript and save data with local storage

Trying to make a seat reservation system with javascript and save data with local storage for a school project.
This what I now have. I have 2dim array with 4 different img(seats) chair one witch is class 1 chair 2 class 2 chair 3 class 3 and chair0 witch is no chair..... But i dont know how to reserve the seat..when I click on it it turns green but i want that when i click again to turn back to the img it had before. And would like if i reserve a seat, when i refresh the page to change the img source (turn grey) meaning its already taken
var currentDiv = document.getElementById("div1");
var zaal = [ [0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0],
[0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3],
[3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3],
[3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3],
[0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0],
[0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0] ];
for (i = 0; i < zaal.length; i++) {
var newLine = document.createElement("br");
currentDiv.appendChild(newLine);
for (j = 0; j < zaal[i].length; j++) {
var newImg = document.createElement("img");
currentDiv.appendChild(newImg);
newImg.style.width = "8%";
newImg.className = "stoelen";
newImg.id = "rij_" + i + "_plaats_" + j;
newImg.onclick = function() {myFunction(this)};
if (zaal[i][j] === 3 ) {
newImg.src = "pictures/reservering/chairs3.png";
}
else if (zaal[i][j] === 2 ) {
newImg.src = "pictures/reservering/chairs.png";
}
else if (zaal[i][j] === 1 ) {
newImg.src = "pictures/reservering/chairs5.png";
}
else{
newImg.src = "pictures/reservering/chairs7.png";
}
}
}
function myFunction(x){
x.src = "pictures/reservering/chairs4.png";
}
I got it !!
this is the solution with local storage! I didn't know you can a variable as key in local storage.
var stoel;
var gereserveerd = localStorage.getItem(stoel);
var stoelPrijs = 0;
var currentDiv = document.getElementById("div1");
var zaal = [ [0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0],
[0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3],
[3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 2, 2, 1, 1, 2, 2, 3, 3, 3],
[3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3],
[3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3],
[0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0],
[0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0] ];
function maakStoelen () {
for (i = 0; i < zaal.length; i++) {
var newLine = document.createElement("br");
currentDiv.appendChild(newLine);
for (j = 0; j < zaal[i].length; j++) {
var newImg = document.createElement("img");
currentDiv.appendChild(newImg);
newImg.id = "rij_" + (i+1) + "_plaats_" + j;
newImg.onclick = function() {reserveerStoel(this)};
if (isGereserveerd(newImg)) {
newImg.setAttribute("src", "pictures/reservering/chairs6.png");
//newImg.src = "pictures/reservering/chairs6.png"
newImg.className = "stoel4";
}
else if (zaal[i][j] == 3 ) {
newImg.src = "pictures/reservering/chairs3.png";
newImg.className = "stoel3";
}
else if (zaal[i][j] == 2 ) {
newImg.src = "pictures/reservering/chairs.png";
newImg.className = "stoel2";
}
else if (zaal[i][j] == 1 ) {
newImg.src = "pictures/reservering/chairs5.png";
newImg.className = "stoel1";
}
else {
newImg.src = "pictures/reservering/chairs7.png";
newImg.className = "stoel0";
}
}
}
}
maakStoelen();
function isGereserveerd(newImg) {
var gevonden = false;
var k = 0;
while( !gevonden && (k < localStorage.length)) {
if (newImg.id == localStorage.key(k)) {
gevonden = true;
}
k++;
}
return gevonden;
}
function reserveerStoel(x){
stoel = x.id;
if (x.getAttribute("src") == 'pictures/reservering/chairs4.png') {
localStorage.removeItem(stoel);
if (x.className == "stoel3"){
x.setAttribute("src", 'pictures/reservering/chairs3.png');
stoelPrijs -= 10;
}
else if (x.className == "stoel2"){
x.setAttribute("src", 'pictures/reservering/chairs.png');
stoelPrijs -= 15;
}
else if (x.className == "stoel1"){
x.setAttribute("src", 'pictures/reservering/chairs5.png');
stoelPrijs -= 20;
}
}
else if (x.getAttribute("src") == 'pictures/reservering/chairs7.png'){
alert("kan niet reserveren");
}
else {
localStorage.setItem(stoel, x.id);
x.setAttribute("src", 'pictures/reservering/chairs4.png');
if (x.className == "stoel3"){
stoelPrijs += 10;
}
else if (x.className == "stoel2"){
stoelPrijs += 15;
}
else if (x.className == "stoel1"){
stoelPrijs += 20;
}
}
alert(x.id);
document.getElementById("totaalPrijsText").innerHTML = stoelPrijs;
}
</script>

Categories