javascript MCQ where i dont know the result plese help me [closed] - javascript

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].

Related

Matrix snake array [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 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.

Sort an array relative to element position in another array using pure javascript [duplicate]

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]);

Array sort function without any argument

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.

zipping array of data in js [duplicate]

This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Closed 8 years ago.
in python:
data = [50, 52, 54, 56, 58]
answer = list(zip(data[0:],data[1:]))
print answer
[(50, 52), (52, 54), (54, 56), (56, 58)]
How can I obtain the same answer in JS?
var data = [50, 52, 54, 56, 58];
var answer = ??
print (answer);
map's callback argument three arguments. The element from the array, the index of the element in the array, and the array itself:
data.slice(1).map(function(el, index) {
return [data[index], el];
});

Sum and organize array of arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assuming i have an array of arrays:
[[2012-12-20, 10], [2012-12-20, 25], [2012-12-19, 10], [2012-12-20, 5]]
How to reorganize this array by distinct DATE summing their respective values, so i have something like this:
[[2012-12-20, 40], [2012-12-19, 10]]
EDIT
I've tried something like this:
var array = [[2012-12-20, 10], [2012-12-20, 25], [2012-12-19, 10], [2012-12-20, 5]];
pastDate = '';
pastVal = '';
for(var data in array) {
var curDate = data[0];
var curVal = data[1];
// ??
// Can't figure out how to organize each value of array organized by distinct date.
// Sorry for any fail, I tottaly sux in arrays.
}
Someone?
Thanks in advance!
Try this :
http://jsbin.com/aNeQArek/2/edit
var g=[['2012-12-20', 10], ['2012-12-20', 25], ['2012-12-19', 10], ['2012-12-20', 5]];
var a={};
for( var i=0;i<g.length;i++)
{
a[g[i][0]] = (a[g[i][0]] || 0) + g[i][1] ;
}
console.log(a);
result : (it's object with your desired values).
{2012-12-20: 40, 2012-12-19: 10}

Categories