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 1 year ago.
Improve this question
How can i write this?!
Create a function that takes a number(it can be any number) and produces a spiral matrix (two-dimensional array) with 7 columns and 6 rows where the start is the number you passed in as an argument. See images below for the pattern. In the first example snakeArray(1) was called. The second one snakeArray(-10).
There are several ways to do this. But since the size of the array is always 6x7, you can actually create a kind of template array as an array literal, which starts with 0.
Then the rest is a piece of cake: just add the argument to all the values:
function snakeArray(start) {
return [
[ 0, 1, 2, 3, 4, 5, 6],
[21, 22, 23, 24, 25, 26, 7],
[20, 35, 36, 37, 38, 27, 8],
[19, 34, 41, 40, 39, 28, 9],
[18, 33, 32, 31, 30, 29, 10],
[17, 16, 15, 14, 13, 12, 11]
].map(row => row.map(val => val + start));
}
let result = snakeArray(-10);
for (let row of result) console.log(...row);
So the [...] array literal creates the array as if the value to start with was 0. Then we iterate over the rows of that array with map, and then we iterate over each value in that row with a nested map. Then we add the start value to that value and return that. The inner map builds a new row with those new values, which is returned in the callback to the outer map. And that outer map returns the new rows as a new matrix, where every value will have been increased with start.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 days ago.
Improve this question
What will be the output of following?
const store new Set(); const age [15, 18, 21, 25); store.add(age); age[1] - 30: store.forEach(e console.log(e));
` Options
[object object) 2) [15, 30, 21, 25] 3) [15, 18, 21, 25]
Run time error `
Output: [15, 18, 21, 25]
Explanation:
store is a Set object that stores unique values.
age is an array with 4 elements: [15, 18, 21, 25].
The store.add(age) method adds the age array to the store Set.
The expression age[1] - 30 subtracts 30 from the second element of
the age array (18), but this expression is not used or stored
anywhere.
The store.forEach method iterates over the elements in the store Set,
and logs each element to the console using console.log(e). The only
element in store is the age array, so it is logged to the console and
the output will be [15, 18, 21, 25].
Actually here you have so many syntax errors, if this code supposed to be a JS code. Probably it should look more like the following:
const store = new Set();
const age = [15, 18, 21, 25];
store.add(age);
age[1] = 30;
store.forEach(e => console.log(e));
Since you call forEach for store (not for age) variable then the answer should be [15, 30, 21, 25].
If the statement age[1] - 30; is correct (not a typo) then it actually do nothing and the correct answer is [15, 18, 21, 25].
This question already has answers here:
sort 2 array with the values of one of them in javascript
(4 answers)
Closed 4 years ago.
Suppose I have two arrays:
arr1 = [64, 23, 35, 11, 55];
arr2 = [34, 10, 54, 12, 4];
If I rearrange (or sort) arr1 then the elements of arr2 should also be rearranged as per the position (or index) of arr1.
For example: if I sort arr1
arr1 = [11, 23, 35, 55, 64];
then the elements in arr2 should be
arr2 : [12, 10, 54, 4, 34 ] (arranged according to index of arr1).
Is it possible? I found that it can be done in same array but I am trying with two different arrays. Thank you for helping.
You could store the original positions before sorting of the first array, then apply that to the second:
// Wrap original positions and values:
const withPos = arr1.map((v, i) => ({v, i}));
// Sort
withPos.sort((a, b) => a.v - b.v);
// Unwrap & apply sort to second array:
arr1 = withPos.map(e => e.v);
arr2 = withPos.map(e => arr2[e.i]);
Can anybody explain how in this case sort works?
var arrayNumb = [2, 8, 15, 16, 23, 42];
arrayNumb.sort();
console.log(arrayNumb); // [ 15, 16, 2, 23, 42, 8 ]
See the MDN documentation:
compareFunction Optional
Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.
This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 6 years ago.
Below is my array if items that I want to reduce it to a single list of arrays..
var input=[
[
2
],
[
3,
13
],
[
4,
14
],
[
5,
15,
25,
35
]
]
var output=[
2,
3,
13,
4,
14,
5,
15,
25,
35
]
My code:
function reduceArray(item){
for(var i=0;i<item.length;i++){
return i;
}
}
var result=result.map((item)=>{
if(item.length>0){
return reduceArray(item);
}else{
return item;
}
})
which produces the same result.Can anyone please figure out where I'm doing wrong or any other approach to achieve this..Thanks
input.reduce(function(a, x) { return a.concat(x); });
// => [2, 3, 13, 4, 14, 5, 15, 25, 35]
reduce sets the accumulator to the first element (or a starting value if provided), then calls the function with the accumulator and each successive element. The function we provide is concatenation. If we say input is [a, b, c], then the above command will be equivalent to a.concat(b).concat(c). [concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) produces a new array by smushing two or more arrays together.
EDIT: Actually, there is another possible answer:
Array.prototype.concat.apply(input[0], array.slice(1));
// => [2, 3, 13, 4, 14, 5, 15, 25, 35]
This directly calls concat with multiple arguments; if input is again [a, b, c], then this is equivalent to a.concat(b, c). apply calls a function with a given receiver and arguments; slice will give us just a part of the array, in this case everything starting from the first element (which we need to chop off since it needs to be the receiver of the concat call).
One liner would be
input = [[2],[3,13],[4,14],[5,15,25,35]];
[].concat.apply([],input);
You can use lodash's flattenDeep()
_.flattenDeep([1, [2, [3, [4]], 5]]);
// → [1, 2, 3, 4, 5]
User concat.check this for more information http://www.w3schools.com/jsref/jsref_concat_array.asp
var input=[[2],[3,13],[4,14],[5,15,25,35]];
var output=[];
for(var i = 0; i < input.length; i++)
{
output = output.concat(input[i]);
}
console.log(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
use concat is the perfect way
The concat() method is used to join two or more arrays.
This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.
var newArr = [];
for(var i = 0; i < input.length; i++)
{
newArr = newArr.concat(input[i]);
}
console.log(newArr);
I'm retrieving some data and the data looks like this:
1, 2, 3, 4, 5
6, 7, 8, 9, 10
11, 12, 13, 14, 15
I want it to look like this
[
[[1],[2],[3],[4],[5]],
[[6],[7],[8],[9],[10]],
[[11],[12],[13],[14],[15]]
]
So that I may address the array like a matrix, data[0][1] would be "2".
Through this answer, it's almost there, but not quite. I'm having trouble getting to look like what I want.
How about this, assuming this accurately represents your input data:
var data = "1,2,3,4,5\n6,7,8,9,10\n11,12,13,14,15";
var matrix = data.split('\n').map(function(val) {
return val.split(',');
});
Note that your specified output is probably not what you meant. Each number should probably not be its own single-item array. Instead, the code above produces:
[
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 1]
]
Which means matrix[0][1] would return 2, as opposed to having to do matrix[0][1][0]
Edit: As noted in the comments, I've left it up to you to ensure this fits your browser-support needs. This also goes for every other line of JS you ever write...
NOTE - If you need to iterate through an array use a simple for, and not a for..in
for..in returns the items in no guaranteed order which is probably not what you want when working with an array
for..in returns not the just the array elements, but anything added to the Array prototype (meaning if you use a traditional for loop you can be completely confident the code will work regardless of what external libraries may be included on the page. No need to worry that some other coder has added properties/methods to Array.prototype)
If \n is the line separator and , is the item seperator within a line, you can use something like:
/* assuming data is already filled like:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
*/
var arr = data.split("\n"), arr2 = [];
for(var i = 0; i < arr.length; i++) {
if(arr[i] != '') arr2.push(arr[i].split(','));
}
console.log(arr2);
/* arr2 will be like:
[
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]
]
*/
var data = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15'.split(',');
var matrixData = [];
while (data.length > 0) {
matrixData.push(data.splice(0, 5));
}
document.write(matrixData[0][1]);
Edit: If you get the data as a simple array of numbers