Merge two arrays alternating value in a functional programming approach [duplicate] - javascript

This question already has answers here:
Merge Two Arrays so that the Values Alternate
(6 answers)
Closed 2 years ago.
How can I merge two arrays in a functional programming approach?
Current
const result = [];
for (let i = 0; i < arr1.length; ++i) {
result.push(arr1[i]);
if (i < arr1.length - 1) {
result.push(arr2[i]);
}
}
This works for I need it to do, but I’d like to see thoughts on my question. I have attempted a few different ways but this is what I think is the cleanest.

function alternatePush (arr1, arr2) {
let myArray = [];
for (let i = 0; i < arr2.length; i++){
myArray.push(arr1[i]);
myArray.push(arr2[i]);
}
return myArray;
}
alternatePush ([1, 3, 5], [2, 4, 6]);

Related

var vs. let inside a for loop [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Javascript infamous Loop issue? [duplicate]
(5 answers)
Closed last month.
The following piece of code will produce 1, 2, 3, 4
const array = [1, 2, 3, 4];
for (let i = 0; i < array.length; i++) {
setTimeout(() => {
console.log(array[i]);
}, 1000);
}
Whereas using var i = 0 in the for loop will produce undefined, undefined, undefined, undefined
const array = [1, 2, 3, 4];
for (var i = 0; i < array.length; i++) {
setTimeout(() => {
console.log(array[i]);
}, 1000);
}
I understand var and let have different scopes but can somebody explain why in the var example, i evaluates to 4 in each iteration?

how to combine two numbers between an array of numbers with javascript? [duplicate]

This question already has answers here:
Given an array, how to generate all combinations of subset size k?
(6 answers)
Closed 1 year ago.
I have an array of numbers and would like to generate all possible combinations with 2 numbers as shown in the expected
const numbers = [1,2,3];
Expected:
const result = [
[1,2],
[1,3],
[2,1],
[2,3],
[3,1],
[3,2]
]
let num = [1,2,3];
let arr = [];
for(let i=0; i<num.length; i++){
for(let j=0; j<num.length; j++){
if(j === i) continue;
arr.push([num[i], num[j]]);
}
}
console.log(arr);

How do I flatten a two-dimensional array with 3 entries in javascript? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 1 year ago.
I was asked to write a function 'flat' that flattens a 2-dimensional array with 3 entries. The function should return an array with 9 elements.
For example, console.log (flat([[1,2,3], [5,7,8], [9,4,6]])) should return
[1,2,3,5,7,8,9,4,6]
My code is like this:
function flat(arr){
let newArr = [];
for (var i =0; i< 3; i++){
for (var j = 0; j< 3; j++){
return newArr.concat(arr[i][j]);
}
}
}
console.log (flat([[1,2,3], [5,7,8], [9,4,6]]));
With this code, I only have the first element, [1], returned in the array newArr. Can anyone help with this problem? Or is there any other way to flatten a 2D array? Thanks.
You can use the built in array method .flat() to do this.
In your example
function flat(array) {
return array.flat()
}
Snippet
function flat(arr) {
return arr.flat()
}
console.log(flat([
[1, 2, 3],
[5, 7, 8],
[9, 4, 6]
]));
function flat(arr){
let newArr = [];
for (var i =0; i< 3; i++){
for (var j = 0; j< 3; j++){
newArr.push(arr[i][j]);
}
}
return newArr;
}
console.log (flat([[1,2,3], [5,7,8], [9,4,6]]));
You need to move the return statement out of the loop, or else it will exit the function the first time it gets to that line.

compute how many times the elements appear in the array [duplicate]

This question already has answers here:
How to count certain elements in array?
(27 answers)
Closed 4 years ago.
input: arbitrary array
output: unique elements and their count
So far I was able to find the unique elements but I can't figure it out how to correlate them with the count for each element. Any suggestions(without using functions if possible - just started and didn't get to functions yet)?
var arr = [3, 4, 4, 3, 3];
var new_arr = [];
for (i = 0; i < arr.length; i++) {
if (new_arr.includes(arr[i])) {
// pass
} else {
new_arr.push(arr[i]);
}
}
console.log(new_arr);
Use an Object instead of an Array to keep track of the count. The key is the number in question and its value is the count.
Use Array#reduce
const res = [3, 4, 4, 3, 3].reduce((acc,cur)=>{
if(acc[cur]) acc[cur]++;
else acc[cur] = 1;
return acc;
}, {});
console.log(res);
Or without any methods:
var arr = [3, 4, 4, 3, 3];
var new_arr = {};
for (i = 0; i < arr.length; i++) {
if (new_arr[arr[i]]) {
new_arr[arr[i]]++;
} else {
new_arr[arr[i]] = 1;
}
}
console.log(new_arr);

Why my solution is not working and returns empty array? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
My code:
function diffArray(arr1, arr2) {
var newArr = [];
var arr = arr1.concat(arr2);
for(var i = 0; i < arr.length; i++)
{
for(var j = (arr.length - 1); j <= 0; j--)
{
if(i == j)
{
continue;
}
else if(arr[i] === arr[j])
{
break;
}
else
{
newArr.push(arr[i]);
}
}
}
// Same, same; but different.
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
What's wrong with my solution?
Alternative, cleaner solution using ES6 features.
const diffArray = (arr1, arr2) => {
const a = arr1.filter(v => !arr2.includes(v));
const b = arr2.filter(v => !arr1.includes(v));
return [...a, ...b];
}
console.log(diffArray([1, 2, 3, 5, 6], [1, 2, 3, 4, 5]));

Categories