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));
Related
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].
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
const array = [1, 2, 2, 2, 3, 4, 4, 4, 4, 5]
// first case
const filteredArray1 = new Set([...array])
// second case
const filteredArray2 = [...new Set(array)]
console.log(filteredArray1, filteredArray2)
const filteredArray2 = [...new Set(array)]
console.log(filteredArray1, filteredArray2)
// This is much suitable way of doing it since you'll be directly getting the type of the new variable as an array. In the first method you have the chance of getting the type as a set or an object.
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.
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(""));
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 4 years ago.
Improve this question
So I am currently attempting to do as the title states, where I have two arrays,
var weapon = ["Stick", "Dagger", "Shortsword", "Longsword", "Magical Sword", "Chunchunmaru"];
and
var weaponAverageDamage = [0, 3, 6, 9, 12, 15];
I would like to have it so that whatever specific index "weapon" is, weaponAverageDamage would be the same thing. For example, any instances in the program where weapon[1] came up, if a monster's health were being calculated or something, then weaponAverageDamage[1] also would be accessed.
Considering the fact that the array weaponAverageDamage is sorted according to the corresponding value to each weapon in weapon array, we can do this:
var weapon = ["Stick", "Dagger", "Shortsword", "Longsword", "Magical Sword", "Chunchunmaru"];
var weaponAverageDamage = [0, 3, 6, 9, 12, 15];
var weaponIndex = weapon.indexOf(weapon[2])
var AvgDamage = weaponAverageDamage[weaponIndex]; //required value