Can someone suggest some better way of writing this Javascript code - javascript

I am given an input array need to convert to output array (it's consecutive array in the output array.)
var input = [1, 3, 4, 5, 8, 9, 15, 20, 21, 22, 23, 24, 25, 26, 40];
var output = [[1], [3, 4, 5], [8, 9], [15], [20, 21, 22, 23, 24, 25, 26], [40]];
I am able to achieve this by:
let t = 0;
let tArr = []
const a = [];
input.map(i => {
if (i-t === 1) {
tArr.push(i);
} else {
a.push(tArr);
tArr = [];
tArr.push(i)
}
t = i;
});
a.push(tArr);
console.log("final", a)
Can someone suggest a cleaner code or if this could be optimized.

You could reduce the array by looking at the index or ar the former value and compare to the actual value.
var input = [1, 3, 4, 5, 8, 9, 15, 20, 21, 22, 23, 24, 25, 26, 40],
result = input.reduce((r, v, i, a) => {
if (!i || a[i - 1] + 1 < v) {
r.push([v]);
} else {
r[r.length - 1].push(v);
}
return r;
}, []);
console.log(result);

Related

Shuffle nested arrays in Javascript

I'm trying to sort multiple arrays within an array (which also has to be shuffled). A simplified example is:
let toShuffle = [
[1, 2, 3, 4, 5],
[9, 8, 7, 6, 5],
[10, 67, 19 ,27]
...
];
const shuffled = shuffle(toShuffle);
// outout would look something like:
// [
// [8, 6, 5, 7, 9],
// [4, 3, 1, 5, 2],
// [19, 26, 10, 67],
// ...
// ]
This needs to be flexible, so any number of arrays with any amount of values should be valid.
Here is what I've tried:
function shuffle(a) {
for (let e in a) {
if (Array.isArray(a[e])) {
a[e] = shuffle(a[e]);
} else {
a.splice(e, 1);
a.splice(Math.floor(Math.random() * a.length), 0, a[e]);
}
}
return a;
}
console.log("Shuffled: " + shuffle([
[1, 2, 3, 4, 5],
[5, 4, 3, 2, 1]
]))
But it's not working as intended. Is their an easier way to do this? Or is my code correct and just buggy.
You can use Array.from() to create a new shallow-copied array and then to shuffle Array.prototype.sort() combined with Math.random()
Code:
const toShuffle = [
[1, 2, 3, 4, 5],
[9, 8, 7, 6, 5],
[10, 67, 19 ,27]
]
const shuffle = a => Array.from(a).sort(() => .5 - Math.random())
const result = toShuffle.map(shuffle)
console.log('Shuffled:', JSON.stringify(result))
console.log('To shuffle:', JSON.stringify(toShuffle))
You almost got it. The problem is that you are removing one item from an array, instead of capturing the removed item and them placing in a random position:
let toShuffle = [
[1, 2, 3, 4, 5],
[9, 8, 7, 6, 5],
[10, 67, 19 ,27]
];
function shuffle(a) {
a = [...a]; //clone array
for (let e in a) {
if (Array.isArray(a[e])) {
a[e] = shuffle(a[e]);
} else {
a.splice(~~(Math.random() * a.length), 0, a.splice(e, 1)[0]);
}
}
return a;
}
console.log(JSON.stringify(shuffle(toShuffle)))
console.log(JSON.stringify(toShuffle))
[EDIT]
The original code did not shuffle the parent array, if you need shuffle everything recursively, you can use this:
let toShuffle = [
[1, 2, 3, 4, 5],
[9, 8, 7, 6, 5],
[10, 67, 19 ,27]
];
function shuffle(a) {
a = a.map(i => Array.isArray(i) ? shuffle(i) : i); //clone array
a.sort(i => ~~(Math.random() * 2) - 1); //shuffle
return a;
}
console.log("shuffled", JSON.stringify(shuffle(toShuffle)))
console.log("original", JSON.stringify(toShuffle))

Write nested map function

I have an object like this:
const object = {
detectors: [1, 2],
responders: [4, 22],
activators: [5, 23, 31],
enablers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
upgraders: [14, 15, 16, 17, 18, 19, 20, 21, 22],
catalyzer: [12, 29],
chains: [27],
trappers: [13],
finishers: [16],
}
Expected output :
[
{
'detectors': 1,
'responders': 4,
'activators': 5,
'enablers': 1,
'upgraders': 23,
'catalyzer': 12,
'chains': 27,
'trappers': 13,
'finishers': 16,
},
{
'detectors': 2,
'responders': 4,
'activators': 5,
'enablers': 1,
'upgraders': 23,
'catalyzer': 12,
'chains': 27,
'trappers': 13,
'finishers': 16,
},
{
'detectors': 1,
'responders': 22,
'activators': 5,
'enablers': 1,
'upgraders': 23,
'catalyzer': 12,
'chains': 27,
'trappers': 13,
'finishers': 16,
},
{...
And I already wrote a function like this:
object.activators.map((activator, i) => {
return object.detectors.map((detector, i) => {
return object.responders.map((responder, i) => {
return {
detectors: detector,
responders: responder,
activators: activator,
};
});
});
});
I can write another function to flatten the output of the code above, but is there any other way to write the code above into a more general function (not hardcoded) that can apply to any object?
You can use a recursive function to get all permutations from the entries.
const object = {
detectors: [1, 2, 3],
responders: [4, 22],
activators: [1, 2, 3, 4]
};
const getPermutations = obj => {
const res = [];
const entries = Object.entries(obj);
const go = (curr, idx) => {
const key = entries[idx][0];
for(const val of entries[idx][1]){
const next = {...curr, [key]: val};
if(idx !== entries.length - 1) go(next, idx + 1);
else res.push(next);
}
};
go({}, 0);
return res;
}
console.log(getPermutations(object));

Array spread values until matches length of another array length, to allow arrays to zip together

I have two arrays a and b.
Either array can have any number of items. However their length may not match.
I need the array lengths to match so I can zip the two array together.
For example:
a = [1, 2, 3, 4]
and
b = [1, 2]
Becomes:
a = [1, 2, 3, 4]
and
b = [1, 1, 2, 2]
I need b to match the length of a or vice versa to whatever one is longer length.
As well as to spread the values of the shorter array until matches the length of the longer array.
The spread on the shorter array would only contain the values present at start.
For example:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and
b = [1, 2]
Becomes
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and
b = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
Another example:
a = [21, 22, 23, 24, 25, 26, 27]
and
b = [39, 40, 41, 42]
Becomes:
a = [21, 22, 23, 24, 25, 26, 27]
and
b = [39, 39, 40, 40, 41, 41, 42]
SOLVED IT using Ramda
const a = [1, 2]
const b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
R.sort(R.gte, R.flatten(R.repeat(a, b.length / 2)))
Without relying on any libraries, this function will give you the desired result
const a = [21, 22, 23, 24, 25, 26, 27]
const b = [39, 40, 41, 42]
// a, b = longer array, shorter array
function spread(a, b) {
let result = null
if (a.length !== b.length) {
// target: array that needs to be spread
const [target, longest] = a.length > b.length ? [b, a] : [a, b]
// difference: amount target needs to be spread
const difference = longest.length - target.length
// check if elements need to be repeated more than twice
if (difference > target.length) {
result = [].concat(
...target.map((n, i) => {
if (typeof n !== 'string') {
return Array.from(n.toString().repeat(difference / 2)).map(Number)
}
return Array.from(n.repeat(difference / 2))
})
)
} else {
// repeat N elements twice until N <= difference/2
result = [].concat(
...target.map((n, i) => (i <= difference / 2 ? [n, n] : n))
)
}
// return the spread array
return result
}
// return original array if both arrays are same length
return b
}
spread(a, b) // => [ 39, 39, 40, 40, 41, 42 ] 
Pure JavaScript solution that will extend a shorter array to the length of a longer one. The stretching is done by repeating each value in the shorter array and dynamically re-calculating how many times this is needed. So with lengths 10 and 3, the shorter array will have the first item repeated three times but the rest only two times in order to fit:
longer length: 10
shorter: [ 1, 2, 3 ]
/|\ /| |\
/ | \ / | | \
result: [ 1, 1, 1, 2, 2, 3, 3 ]
function equaliseLength(a, b) {
const [shorter, longer] = [a, b].sort((x, y) => x.length - y.length);
let remaining = longer.length;
const stretchedArray = shorter.flatMap((item, index) => {
//how many we need of this element
const repeat = Math.ceil(remaining / (shorter.length - index));
//adjust the remaining
remaining -= repeat;
//generate an array with the element repeated
return Array(repeat).fill(item)
});
//return to the order of the input:
//if `a` was the longer array, it goes first
//otherwise flip them
return longer === a ?
[longer, stretchedArray] :
[stretchedArray, longer]
}
console.log(printResult(
[1, 2, 3, 4],
[1, 2]
));
console.log(printResult(
[21, 22, 23, 24, 25, 26, 27],
[39, 40, 41, 42]
));
console.log(printResult(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2]
));
console.log(printResult(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3]
));
console.log(printResult(
[1, 2, 3],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
));
console.log(printResult(
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
));
//just to make the display better
function printResult(a, b) {
const [resultA, resultB] = equaliseLength(a, b)
.map(x => x.map(y => String(y).padStart(2)))
.map(x => x.join("|"))
return `a = ${JSON.stringify(a)} b = ${JSON.stringify(b)}
result:
a = |${resultA}|
b = |${resultB}|`;
}

Break an array into smaller arrays, using a specific character as a break point

I have an array like this:
[ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ]
I want to break it into subarrays based on where the '-' is, so it would look like this:
[ [3, 8, 18], [19, 3, 8, 20, 19, 3, 8], [22] ]
I need to write a function to do this for other similar arrays.
I have tried using the slice method but I can't quite figure out how to make it work. Any ideas would be appreciated.
you can iterate over your array and fill a temp-array with the content, when you find your special split-character, you can push the temp array into a result-array, empty the temp-array and then continue the loop.
let data = [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ];
let result = [];
let temp = [];
for (let item of data) {
if (item == '-') {
result.push(temp);
temp = [];
} else {
temp.push(item);
}
}
result.push(temp);
console.log(result);
I believe this is the answer you are looking for:
l = [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ];
let res = [];
let subres = [];
l.forEach((n, index) => {
if (n !== '-') {
subres.push(n);
} else {
res.push(subres);
subres = [];
}
if (index === l.length - 1 && subres.length >= 1) {
res.push(subres);
}
})
console.log(res);
You can use reduce:
const arr = [3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22];
const res = arr.reduce((acc, curr) => {
curr == "-" ? acc.push([]) : acc[acc.length - 1].push(curr);
return acc;
}, [[]]);
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
You can do a function to do this for you and either inside use Array.reduce or Array.forEach to go through each of the elements and "group" based on the location of a passed in character.
var data = [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ]
let chunkByChar = (arr, char) => {
let result = [[]]
arr.forEach(x => x === char ? result.push([]) : result[result.length-1].push(x))
return result
}
console.log(chunkByChar(data, '-'))
console.log(chunkByChar(data, 8))
console.log(chunkByChar(data, 3))

JavaScript. How to Compare Input Arrays

I'm stuck with this problem for 3 days now... Someone please help me.
Challenge 5
Construct a function intersection that compares input arrays and returns a new array with elements found in all of the inputs.
function intersection(arrayOfArrays) {
}
console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
// should log: [5, 15]
Reduce the arrays to a Map of counts, with the value as key. Spread the Map to entries. Use Array.filter() on the Map's entries to remove all entries, which value is not equal to the arrayOfArrays lenth. Extract the original number from the entries using Array.map():
function intersection(arrayOfArrays) {
return [...arrayOfArrays.reduce((r, s) => {
s.forEach((n) => r.set(n, (r.get(n) || 0) + 1));
return r;
}, new Map())]
.filter(([k, v]) => v === arrayOfArrays.length)
.map(([k]) => k);
}
console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
You could reduce the array by filtering with just checking if the other array contains the value.
This works for arrays with unique values.
Array#reduce:
If no initialValue is provided, then accumulator will be equal to the first value in the array, and currentValue will be equal to the second.
The callback
a.filter(v => b.includes(v))
filters array a. If the array b includes the value of a, then this value v is included in the accumulator for the next iteration or as final result.
accumulator currentValue new accumulator
a b result
-------------------- -------------------- --------------------
[ 5, 10, 15, 20] [15, 88, 1, 5, 7] [ 5, 15]
[ 5, 15] [ 1, 10, 15, 5, 20] [ 5, 15]
function intersection(arrayOfArrays) {
return arrayOfArrays.reduce((a, b) => a.filter(v => b.includes(v)));
}
console.log(intersection([[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]]));
First try to find out the intersection of two arrays which is the base problem. Then try to build up for variable number of arrays passed as arguments for intersection. You can use reduce() for doing that.
function intersectionOfTwoArrays(arr1, arr2)
{
return arr1.filter(x => arr2.some(y => y === x));
}
function intersection(...arrayOfArrays)
{
return arrayOfArrays
.reduce((a, b) => intersectionOfTwoArrays(a, b));
}
intersection(
[5, 10, 15, 20],
[15, 88, 1, 5, 7],
[1, 10, 15, 5, 20]
);
You can go through the first array in the array of arrays and check which of its value is present in all the other arrays.
Here is an example:
function intersection(input) {
let firstArray = input[0];
let restOfArrays = input.splice(1);
return firstArray.filter(v => restOfArrays.every(arr => arr.includes(v)));
}
const input = [[5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]];
const result = intersection(input);
console.log(result);
Works with even if there is duplicate in same array.. like in my example added 5 twice in arrayEle[1];
var arrayEle = [[5, 10, 15, 20], [15, 88, 1, 5, 5], [1, 10, 15, 5, 20]]
var startIndex = 1;
var newArray = [];
for (var x = 0; x < arrayEle[0].length; x++) {
var temVal = 1;
var value;
for (var y = 1; y < arrayEle.length; y++) {
for (var z = 0; z < arrayEle[y].length; z++) {
if (arrayEle[y][z] == arrayEle[0][x]) {
temVal++;
value = arrayEle[y][z];
break;
}
}
}
if (temVal == arrayEle.length) {
newArray.push(value);
console.log(value);
}
}
console.log(newArray);
//log: [5, 15]
I think you want the common elements. Let me show you how:
var Array1 = [5, 10, 15, 20]
var Array2 = [15, 88, 1, 5, 7]
var Array3 = [1, 10, 15, 5, 20]
var found = []
var Final = []
var c = 1;e = 1;
for (i = 1;i<=Array1.length;i++){
for (k = 1;k<=Array2.length;i++){
if (Array1[i] == Array2[k]){
Found[c] = Array[i];
c++;
}
}
}
for (n = 1;n <= Found.length ; n++){
for (m = 1;m <= Array3.length ; n++){
if (Found[n] == Array3[m]){
Final[e] = Found[n]
e++;
}
}
}
//the Array Final Contains 5 , 15

Categories