This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: min & max Array values?
Generate max 'N' values from javascript array
var arr = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
How to execute 5 max values from this array?
result must be like
10, 9, 8, 7, 6
Check out the Arrays Java API doc. It'll give you some practice in reading up on documentation as well as give you methods to use to solve the homework problem ;)
you can try this
1. first you arrange Array in ascending order.
2. get it's Length.
3. get last 5 Digits whose u have required.
i thinks this is helpful to you.
..! cheers
Related
This question already has answers here:
How does '&' work in relation to odd and even? In JS
(3 answers)
Closed last month.
I'm working through a problem on CodeSignal and trying to understand some of the solutions that other people have submitted. One of the solutions was as follows, and I don't understand what the ampersand is doing.
(a) => a.reduce((p,v,i) => (p[i&1]+=v,p), [0,0])
The problem is:
Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.
You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.
Example
For a = [50, 60, 60, 45, 70], the output should be
solution(a) = [180, 105].
In this solution, the & operator is used to perform a bitwise AND operation. In JavaScript, the & operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
In the given solution, the & operator is used to determine whether the index i of the current element in the array is even or odd. If i is even, the result of i & 1 will be 0. If i is odd, the result of i & 1 will be 1.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
here is the problem
multiDimensionalArray = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12, [13, 14, [15]]]]]];
I want to access number 15. I cant seem to figure it out.
p.s i am a newbee with js
multiDimensionalArray[3][3][3][3][2][0]
Each [n] is accessing an array value at index n.
So the above statement says:
1) take the 4th (index 3 is the 4th element due to indices starting at 0, not 1) element.
2) This happens to be an array. Take the 4th element again.
3) ...
This question already has answers here:
Javascript Equivalent to C# LINQ Select
(8 answers)
Closed 6 years ago.
I am new to Javascript and I was wondering is there a similar function in Javascript like C# Select(). My task is from array of people to sort the age of them and select only age of each person and print it. And this is what i come up with:
ageArraySorted = args.sort(function(person1, person2) {
return person1.age - person2.age;
});
I sorted them and now I need only the values of age property to be printed.
Without a library like linq.js the closest analog is the map method on Array;
ageArraySorted = args.sort(function(person1, person2) {
return person1.age - person2.age;
}).map(function(item) {
return item.age;
});
be careful with Map as a new to javascript
map does not mutate the array on which it is called (although callback, if invoked, may do so).
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
and map was added to the ECMA-262 standard in the 5th edition;
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.5 (1.8) 9 (Yes) (Yes)
from ...https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
I'm learning about the map() method right now and I understand very basic examples.
var numbers = [2, 4, 6];
var double = numbers.map(function(value) {
return value * 2;
});
My question is, in what cases do developers use the map() method to help solve problems? Are there some good resources with real world examples?
Thanks for the help!
As #Tushar referred:
The map() method creates a new array with the results of calling a
provided function on every element in this array.
So it is basically used when you need to apply certain functionality to every single element of an array and get the result back as an array with the new results.
For example doubling the numbers:
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]. numbers is still [1, 4, 9]
It basically helps to shorten your code eliminating the need of using for loop. But do remember it is used when every element of the array is manipulated because map() generates similar length of array provided.
For eg.- in the example you provided doubles will have [2, 8, 18].
where 2 correspond to 1.
4 correspond to 8.
9 correspond to 18.
I recommend you to watch the whole video but your answer is at the 14th minute:
Asynchronous JavaScript at Netflix by Matthew Podwysowski at JSConf Budapest 2015
This isn't homework, purely for my side project.
I'm implementing the Permutation Crossover Operator for genetic algorithm (solving travelling salesman, where each number represents a city index) and I'm having some problem with the boundary case when there isn't a 1-for-1 mapping.
Consider the two genomes below, and assuming the final two entries are switched. Thus, 5 is mapped to 6, and 6 is mapped to 7. So what happens when I hit a number 6 - should I change it to 5 or 7, and this can lead to an invalid tour where a city is visited twice.
//initial case
GenomeA: [ 2, 3, 1, 4, 0, 7, 5, 6 ]
GenomeB: [ 1, 2, 0, 3, 4, 5, 6, 7 ]
5 <--> 6
6 <--> 7
//after mapping
GenomeA: [ 2, 3, 1, 4, 0, 6, 6, 7 ]
GenomeB: [ 1, 2, 0, 3, 4, 6, 5, 6 ]
Should I randomly select another number if a number is already mapped? Or should I not switchover any numbers that already has been mapped?
For instance,
a) Evaluate first set of numbers (5 <--> 6)
b) Since 5 has not been mapped, map 5 to 6 and vice versa
c) Evaluate second set of numbers (6 <--> 7)
d) Since 6 is already mapped to 7, ignore this set of numbers
I found an easy way to produce legitimate offspring in the case of multiple mappings (5 <--> 6 <--> 7).
a) First check if any number outside the substring exchanged ("originalNumber" is contained within the mapping
b) Let the mapped value of "originaNumber" be called "replacement"
c) Check if "replacement" is also contained within the mapping (if it is, this implies that there will be a clash if we set "originalNumber" to "replacement") If it isn't, simply set "originalNumber" to "replacement"
d) Otherwise, find the mapped value of "replacement" and set "originalNumber" to it.