How do I turn this string into an array of numbers? - javascript

I'm writing this code where a user inputs an array in a input box, so the box pretty much contains a string that looks somewhat like this: [2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], [2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], [2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2] etc. etc.
I want to convert this into an array of arrays of numbers so that my code can do something with this, but I'm not sure how.
Simply grabbing the value of the input box outputs something like this: '[2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], \n[2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], \n[2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2]', and when I attempt to split it/regex replace something, either I get nothing, or I get arrays that look like this: ["2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2"]. I'm not quite sure how to fix this, and the code I am using needs it as numbers.
I don't know what to do, so any ideas would be helpful. Thanks!

You can use JSON.parse:
const str = '[2, 2, 2, 2, 2, 9, 9, 9, 2, 2, 2], \n[2, 1, 1, 1, 1, 9, 9, 9, 1, 1, 2], \n[2, 1, 1,14, 1, 9, 9, 9, 1, 1, 2]';
const arrs = JSON.parse('[' + str + ']')
console.log(arrs)

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

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

Adding one line in sudoku checker break my code

function validSolution(board){
for(i=0;i<board.length;i++){
if (new Set(board[i]).size != 9) return false
//if (new Set(getVertical(i,board)).size != 9) return false
}
return true
}
let getVertical = (num,board) => {
let result = []
for(i=0;i<board.length;i++){
result.push(board[i][num])
}
console.log(result)
return result
}
I am working on sudoku checker if I am adding the commented line of code the tests that were passing are failing :( like the first 'if' statement disappeared
sample input (should be false) and it is until adding second 'if' statement
[[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 0, 3, 4, 8],
[1, 0, 0, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 0, 2, 0],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 0, 1, 5, 3, 7, 2, 1, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 0, 0, 4, 8, 1, 1, 7, 9]]
The propblem is that you declare variable i without let keyword.
That means i is a global variable, so in getVertical function i store value 8 and main loop in validSolution skip to end and returns true

Jquery difference between 2 arrays

How i can get the difference of two arrays ?
I think need compare the index and delete the position?
The array cann have letter (example Months)
// Round 1
var array1 = [];
var array2 = [10, 2, 3, 5];
//diff: 10, 2 ,3 ,5
// Round 2
var array1 = [10, 2, 3, 5];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9];
//diff: 2, 5, 11, 9
// Round 3
var array1 = [10, 2, 3, 5, 2, 5, 11, 9];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9, 1, 5, 12, 10];
//diff: 1, 5, 12, 10
var array1 = [10, 2, 3, 5, 2, 5, 11, 9];
var array2 = [10, 2, 3, 5, 2, 5, 11, 9, 1, 5, 12, 10];
var diffArray= array2.splice(array1.length,array2.length-array1.length);
console.log(diffArray)
assuming array1 is subArray of array2

Categories