Array is not defined - Codewars Kata Round Robin Tournament - javascript

I am attempting to do this Kata - https://www.codewars.com/kata/organize-a-round-robin-tournament/train/javascript.
The task is to create a function that organizes a round robin tournament.
Example:
buildMatchesTable(4)
Should return a matrix like:
[
[[1,2], [3, 4]], // first round: 1 vs 2, 3 vs 4
[[1,3], [2, 4]], // second round: 1 vs 3, 2 vs 4
[[1,4], [2, 3]] // third round: 1 vs 4, 2 vs 3
]
I have created a very clunky solution that works up until the last hurdle. I am left with an array (arr6) that is an array listing all the matchups in the correct order, but as a simple array, not subarrays designating the various rounds. So I tried to create a function cut to produce an array in the correct format and it tells me arr7 is not defined.
My solution is poorly written as I am new to this, but I think the fix should be relatively simple, something to do with not returning values correctly in functions, or functions called in the wrong order. Thanks.
function buildMatchesTable(numberOfTeams) {
let n = numberOfTeams; let h = n/2; let arr = []; let arr2 = [[],[]];
let arr3 = [...Array(n-1)].map(v => v); let arr4 = [];
//create array
for (var i = 1; i <= n; i++) {arr.push(i)} //[1, 2, 3, 4]
//split array
arr2[0] = arr.splice(0, arr.length/2);
arr2[1] = arr.splice(0, arr.length); //[[1, 2], [3, 4]]
//create a function that produces matches in a round from array[i]
function round (arr2) {
for (var i = 0; i < arr2[0].length; i++){
arr4.push([arr2[0][i], arr2[1][i]]);
}
arr2 = arr4;
return arr2; // [[1 v 3], [2 v 4]] etc.
}
//create a function that tranforms the arr to gameweek +1 (rotate everything clockwise apart from team 1
function trans(arr2){
//create new arr5 that is the same as arr2
arr5 = [[],[]];
for (var i = 0; i < arr2[0].length; i++) {
arr5[0].push(arr2[0][i])
arr5[1].push(arr2[1][i])
}
//rotate every element apart from arr2[0,0] : [[1, 3], [4, 2]]
let h = arr2[0].length - 1;
arr2[0][1] = arr5[1][0];
arr2[1][h] = arr5[0][h];
for (var i = 2; i <= h; i++){
arr2[0][i] = arr5[0][i-1];}
for (var i = 0; i <= h-1; i++){
arr2[1][i] = arr5[1][i+1];}
return arr2;
}
function final (n, arr2, arr3){ //putting all the functions together
for (var i = 0; i < n-1; i++){
arr3[i] = (round(arr2));
trans(arr2);
}
return arr3; // [[1, 3], [2, 4, [1, 4], [3, 2], [1, 2], [4, 3]] X 3
}
final(n, arr2, arr3)
let arr6 = arr3[0]; // arr6 = [[1, 3], [2, 4, [1, 4], [3, 2], [1, 2], [4, 3]]
function cut(arr6, n) {
let arr7 = [];
let index = 0;
while (index < arr6.length) {
arr7.push(arr6.slice(index, n/2+index));
index += n/2;
}
return arr7;
}
cut(arr6, n);
console.log(n);
console.log(arr);
console.log(arr2);
console.log(arr3[0]);
console.log(arr4);
console.log(arr6);
console.log(arr7);//not working!
//return arr7
}
buildMatchesTable(6)

No wonder, you are declaring let arr7 = []; inside a function. Get it out of the function, on the same level as arr6, arr4, etc.

Related

How to create nested array of different length from a multidimensional array

I have an array that looks like this:
arr = [[1,2,3],
[4,5,6],
[7,8,9]]
I have initialized an empty array and I want put the diagonals of the arr inside the new array. So i've tried this:
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
new_arr = [];
tail = arr.length - 1;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (j == i) {
new_arr.push(arr[i][j]);
}
if (j == tail) {
new_arr.push(arr[i][j]);
tail--;
}
}
}
console.log(new_arr)
The logic seems to work but I can't seem to get the structure right. What I want is to nest two arrays inside the new array like this:
[[1,5,9],[3,5,7]]
But what I get is one array with the right values unordered. How to get the expected output? Any help is appreciated. Thanks!
You need to have 2 separate temporary arrays. And you don't need nested loops. You can optimize the code like this with a single loop if you understand the math.
arr = [[1,2,3],
[4,5,6],
[7,8,9]];
function findDiagonals(arr) {
const diagonal_1 = [];
const diagonal_2 = [];
for( let i = 0; i < arr.length; i++ ) {
diagonal_1.push(arr[i][i]);
diagonal_2.push(arr[i][arr.length - (i+1)]);
}
return [diagonal_1, diagonal_2];
}
console.log(findDiagonals(arr));
var arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
var diagonal1 = [];
var diagonal2 = [];
for (var i = 0; i < arr.length; i++) {
diagonal1.push(arr[i][i]);
diagonal2.push(arr[i][arr.length - i - 1]);
}
var new_arr = [diagonal1, diagonal2];
console.log(new_arr)
The following solution would work for you if the width and height of the number matrix is always equal.
const arr = [[1,2,3],
[4,5,6],
[7,8,9]];
const result = [[],[]];
arr.map((row,index) => {
result[0].push(row[0+index]);
result[1].push(row[row.length - index - 1]);
});
console.log(result); // [[1,5,9],[3,5,7]]
This is a O(n) approach by using the function Array.prototype.reduce.
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const result = arr.reduce(([left, right], array, row, {length}) => {
return [[...left, array[row]], [...right, array[length - row - 1]]];
}, [[],[]]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
the difficulty is low:
const
arr = [[1,2,3],[4,5,6],[7,8,9]]
, new_arr = [[],[]]
;
let p0 = 0, p1 = arr.length
for( let inArr of arr)
{
new_arr[0].push( inArr[p0++] )
new_arr[1].push( inArr[--p1] )
}
console.log( JSON.stringify( new_arr ))
in a one Line code:
const
arr = [[1,2,3],[4,5,6],[7,8,9]]
, new_arr = arr.reduce(([lft,rgt],A,i,{length:Sz})=>([[...lft,A[i]],[...rgt,A[Sz-i-1]]]),[[],[]])
;
console.log( JSON.stringify( new_arr )) // [[1,5,9],[3,5,7]]

Finding the symmetric difference in a loop

I need to write a function that can take an indefinite number of arrays containing integer numbers and it should return 1 array which is the accumulative symmetrical difference between those arrays. Only two arrays are compared at a time. So [1, 2, 3], [3, 4, 2], [1, 5, 3] would first result in [1, 4], (comparing the first two arrays), which is then compared to the third and the final result is [4, 5, 3]. I created a loop that does that for the first two arrays, but I don't know how to turn it into an actual loop that performs the same operation on each step. For some reason using arr[i] and arr[i + 1] throws an error. Here's my code so far.
function test(...arr) {
let accumulator;
for (let i = 0; i < arr.length; i++) {
let common = arr[0].filter(a => arr[1].includes(a))
let arr0 = arr[0].filter(a => !common.includes(a))
let arr1 = arr[1].filter(a => !common.includes(a))
let merged = [...arr0, ...arr1]
accumulator = merged
}
return accumulator
}
console.log(test([1, 2, 3], [3, 4, 2], [1, 5, 3]))
Here accumulator is [1, 4], so at this point the entire operation needs to be performed with the next array and the accumulator, which is where I'm stuck at.
You're iterating with i from 0 to arr.length - 1. arr[i + 1] is arr[arr.length] in the last iteration. It's out of bounds. You could change the loop condition to i < arr.length - 1.
Example:
function test(...arr) {
let accumulator;
for (let i = 0; i < arr.length - 1; i++) {
let common = arr[i].filter(a => arr[i + 1].includes(a))
let arr0 = arr[i].filter(a => !common.includes(a))
let arr1 = arr[i + 1].filter(a => !common.includes(a))
let merged = [...arr0, ...arr1]
accumulator = merged
}
return accumulator
}
console.log(test([1, 2, 3], [3, 4, 2], [1, 5, 3]))

Javascript Multiplying Within an Array

I have an array with multiple arrays inside. I would like to multiply the contents of each array.
Here is my code--currently not getting anything in console.
The desired output is 3 numbers-- the result of multiplication of the three arrays
var arr = [
[1, 2, 3],
[3, 4],
[7, 8, 9]
]
var mult = 1
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr.length; j++) {
mult *= arr[i][j]
}
console.log(mult)
}
You could map the result of the nested multiplying.
const
multiply = (a, b) => a * b,
array = [[1, 2, 3], [3, 4], [7, 8, 9]],
result = array.map(a => a.reduce(multiply, 1));
console.log(result);
You can try this, it is easier using map and reduce functions. arr.map(array => array.reduce(arrayMult, 1)) is the multiplication of each inner array, like [ 6, 12, 504 ], and the last reduce is to multiply these values.
var arr = [[1, 2, 3],[3, 4],[7, 8, 9]];
const arrayMult = (prev, curr) => prev * curr;
const total = arr.map(array => array.reduce(arrayMult, 1)).reduce(arrayMult, 1);
You should iterate over rows. So you need to change inner loop on this :
for (j = 0; j < arr[i].length; j++)
const array = [
[1, 2, 3],
[3, 4],
[7, 8, 9]
];
for (i = 0; i < array.length; i++) {
let result = 1;
for (j = 0; j < array[i].length; j++) {
result *= array[i][j]
}
console.log(result);
}
Nothing wrong with good old iterating; your existing approach only had one bug, on the inner loop you were still checking the length of the top-level array instead of the inner array; j should range from 0 to arr[i].length, not arr.length.
An alternative approach uses map and reduce; comments below explain how it works:
var arr = [
[1, 2, 3],
[3, 4],
[7, 8, 9]
]
console.log(
arr.map(a => { // <-- calls a function on each element of arr,
// leaving the results in an array
return a.reduce( // <-- reducer is like map, calling a function
// on each element of a, but collects its results
// in a "collector" variable passed to each step
(val, sum) => {return val * sum}, // <-- multiply this value with
// the ones so far
1 // <-- initial value for the collector
)
})
)
One approach was to iterate the array of arrays via map which for each array puts the product of all of its items where the latter gets achieved via a multiplying reduce task.
console.log([
[1, 2, 3],
[3, 4],
[7, 8, 9],
].map(arr =>
arr.reduce((x, y) => x * y)
));

Using reduce method to get the multiplication of the last elements of each subarray

This is the array: [[1, 2], [1, 3], [1, 4]]
This is how it looks like with a regular for loop:
let multiplication = 1;
for (let i = 0; i < l.length; i++) {
multiplication *= l[i][1];
}
How do I do the same thing with reduce instead?
Initiate value at the second parameter of reduce, and with the callback function, remember to return the accumulated value
Below solution could help you
const arr = [
[1, 2],
[1, 3],
[1, 4],
];
const res = arr.reduce((acc, el) => acc * el[1], 1);
console.log(res);

Is there an efficient algorithm in JavaScript to find the number of distinct arrays within a larger array set?

Given the following array
let array = [[1, 2], [1, 2], [3, 4], [5, 6], [2, 1]]
I want to return the number of distinct arrays in this set. So the example above should return 3. How do I achieve this? I tried the code below, but it does not give the right answer
let distinct = 0
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length - i; j++) {
let difference = ingredients[i].filter(x => !array[j].includes(x))
if (difference.length > 0) {
distinct += 1;
}
}
}
return distinct;
If the order inside a sub item matters
Use Array.map() to convert each sub-array into a string (I've used String() as suggested by #trincot), create a Set from the array to remove duplicates, and get the size of the Set:
const array = [[1, 2], [1, 2], [3, 4], [5, 6]]
const distinct = new Set(array.map(String))
console.log(distinct.size)
If the order doesn't matter
Sort each sub item, and then convert to string:
const array = [[2, 1], [1, 2], [3, 4], [5, 6]]
const distinct = new Set(array.map(o => String(o.sort())))
console.log(distinct.size)
I didn't check your algorithm at all, just corrected your syntax. Is this what you meant to write?:
let array = [
[1, 2],[1, 2],[3, 4],[5, 6]
];
console.log(countDistinctArrays(array));
function countDistinctArrays(parentArray) {
let distinct = 0;
for (let i = 0; i < parentArray.length; i++) {
for (let j = i + 1; j < parentArray.length - i; j++) {
let difference = parentArray[i].filter(x => !parentArray[j].includes(x))
if (difference.length > 0) {
distinct += 1;
}
}
}
return distinct;
}
Here is how you could have tried. Try converting the inner arrays to a string, then filter the dupes and parse the string again
var pp = [[1, 2], [1, 2] ,[3, 4] ,[5, 6]];
var distict = pp.map(ar=>JSON.stringify(ar))
.filter((itm, idx, arr) => arr.indexOf(itm) === idx)
.map(str=>JSON.parse(str));
console.log(distict.length);

Categories

Resources