How to compare length of three array? Javascript [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to return false or alert or console.log('no true') if three list there is not the same number of items?
example
arrOne = [1,2,3];
arrTwo = [5,6,7];
arrTthree = [1,9];
FALSE arrThree has 2 values , one and two have 3 values.
If same return true.

I have similar idea like #geofh, but it need not wrap all your arrays into another one as input.
function compare(...arrays) {
return arrays.every(arr => arr.length === arrays[0].length);
}
With it, you could invoke compare([1, 2, 3], [5, 6, 7], [1, 9], ...)

OK, I owe you an answer since I erroneously marked it as duplicate. To generalize, you need a sameLength function that takes an array of arrays, perhaps. That could look like:
function sameLength(arrays) {
return arrays.slice(1).every(arr => arr.length === arrays[0].length)
}
so sameLength([arr1, arr2, arr3]) would return false in your case.

Related

Get the splice parameter that converts array A to array B [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I want to write a method to turn array A into array B by a single splice operation, returning the format [index, deleteCount, . . items], which can be used to add or delete elements, but I need to make sure that the minimum number of elements is reached in each operation.
For example, if array A is [1, 2, 2, 3] and array B is [1, 2, 5, 3], then the return result is [2,1,5].

Function with maximum possible sum of some of its k consecutive numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
As title, can anyone help me write a function return maximum possible sum of some of its k consecutive numbers
(numbers that follow each other in order.) of a given array of positive integers. Thank you!
I have read the answer but can anyone show me how its work? i just dont understand those code about?
There are several ways to do this, you can do this with the help of traditional for, Math.max(), indexOf() and Array#reduce.
First of all, you need to find the maximum value of your input array, then you should pop it and according to the iteration count, iterate to find the next maximum value. Then after you find all the maximum values you need to sum them up at last.
function maxOfSumChain(arr, length) {
const maxArr = [];
for (let i = 0; i < length; i++) {
const max = Math.max(...arr);
maxArr.push(max);
arr.splice(arr.indexOf(max), 1);
}
return maxArr.reduce((a, b) => a + b, 0);
}
console.log(maxOfSumChain([1, 3, 2, 6, 2], 3));
console.log(maxOfSumChain([1, 3, 2], 2));

How to return array without square brackets or commas [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array and I want to return it with the commas and the square brackets removed from the returned value so that I can more easily analyze it and use its contents to make a sentence for a user to read better rather than a confusing script for those who don’t know JavaScript. Is there an efficient way to do this?
var a = [5, 6, 7, 3, 1];
console.log(a);
//-> [5, 6, 7, 3, 1] | I want 56731
Use .join(). This returns a string which is the array's elements joined together with nothing.
var a = [5, 6, 7, 3, 1];
console.log(a.join(""));

JavaScript, how to create an array that sequentially counts till N? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Not sure how exactly to word this, but essentially, I'm trying to write a function that if you input 5, it will return [1, 2, 3, 4, 5], if you input 7, it will return [1, 2, 3, 4, 5, 6, 7] etc etc.
It seems simple but for some reason it's just not clicking for me.
Use a for-loop that pushes to an array:
function count(n){
let arr = [];
for (let i = 1; i <= n; i++){
arr.push(i);
}
return arr;
}
console.log(count(5));
console.log(count(7));

Best way to check if an existing array is having all distinct elements or not in JavaScript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Something like this:
let a = [2, 34, 'dafsd', null, {}];
let b = [null, null] -or- ['same','same'] -or- [100, 100]
isDistinct(a) // => true
isDistinct(b) // => false
You could take a Set of the items and check the length of the array against the size of the set. If equal, then all elements are unique.
let a = [2, 34, 'dafsd', null, {}];
console.log(a.length === new Set(a).size);

Categories

Resources