Split integer to array based on size using 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
It may be silly but i need to write an logic to get integer to split to array based on size.
For example if integer is 13 i need to convert to array as [5,5,3] if my size is 5.
if size is 10 then array should be [10,3].
Can any one help me writing the logic in javascript?.

You may build up an array of size N/n if there's no remainder from division (N%n), or N/n+1 if there is one.
Then you may simply Array.prototype.map() through that array and populate the result with n or use remainder as the last element if one exists:
const num = 13,
size = 5,
splitNumber = (N, n) =>
[...Array(0|N/n+(N%n ? 1 : 0))]
.map((_,i,s) =>
N%n && i == s.length-1 ? N%n : n)
console.log(splitNumber(13,5))
console.log(splitNumber(13,10))
.as-console-wrapper{min-height:100%;}

Related

Javascript: check multiple elements in 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 9 months ago.
Improve this question
I have an array: a=[10,11,12,13]. How can check elements from a[0] to a[2] if they >0 in the shortest way and don't use for loop? Does anyone have any ideas? Thank you.
You can slice the array to get the items within the specified indexes, then use filter to filter out the items that don't match the condition and check whether the length of the resulting array is the length of the range.
const a = [10, 11, 12, 13]
const isValid = a.slice(0, 3).filter(e => e > 0).length == 3
//last index in slice is exclusive
console.log(isValid)

javascript : Sort array with number and alphabetical [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
I'm looking for a way to sort an array with words and numbers on each array by highest number
E.g.
'jimmy=40'
'bobby=100'
'cattie=30'
'marrie=50'
'jonny=60'
turns into
'bobby=100'
'jonny=60'
'marrie=50'
'jimmy=40'
'cattie=30'
Is there any solutions for this problem ?
You can split the array items with = and compare the number part inside sort() event handler function:
var arr = ['jimmy=40', 'bobby=100', 'cattie=30', 'marrie=50', 'jonny=60'];
function sortData(data){
return data.sort(function(a, b){
return b.split('=')[1] - a.split('=')[1];
});
}
console.log(sortData(arr));
You can do it in three steps.
First split the array items by equal sign.
Sort the array based on number.
Again join array items by equal sign.
let data = ["jimmy=40", "bobby=100", "cattie=30", "marrie=50", "jonny=60"];
let ret = data
.map((x) => (x = x.split("=")))
.sort((x, y) => y[1] - x[1])
.map((x) => x.join("="));
console.log(ret);

Comparing a string array with object 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 3 years ago.
Improve this question
I want to compare two arrays
array1=["123',"456"];
array2=[{id":"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name"baak"},{id:"456",name"sxs"}];
my objective is to extract the objects from array2 whose ids match the values in array1.
Can someone help me with the optimal solution?
First of all your second array (array2) is syntactically invalid.
You can try with Array.prototype.filter() and Array.prototype.includes()
var array1=["123","456"];
var array2 = [{id:"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name:"baak"},{id:"456",name:"sxs"}];
var res = array2.filter(i => array1.includes(i.id));
console.log(res);

How to calculate all possible results in a 4x3 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 4 years ago.
Improve this question
assume that the array looks like this.
var res = [2,4,1,6,
1,1,2,8,
5,6,7,1];
all possible four quadrants combinations of this array are 81.
in this example we have only one combination on index : 4,5,2,11 which is four 1.
my question is how to calculate them.
thanks.
I'm not completely clear on what you're trying to achieve. Do you want to find every subset of length exactly 4 which contains all the same value? If so, you can do this in N^2 time with the following naive algorithm:
let quadrants = [];
res.forEach(checkElement => {
let possibility = [];
res.forEach((element, index) => {
if (element === checkElement) {
possibility.push(index);
}
});
if (possibility.length === 4) {
quadrants.push(possibility);
}
});
If you want to account for the possibility of the original array having more than 4 of the same number and include all subset quadrants, you'll need to change the length check to >=4 and add one more step at the end of this: calculate the power set of all listed quadrants with length greater than 4, filter out the ones that aren't length 4, and then concatenate those to the quadrants array. (You'll want to remove each quadrant with length >4 from the quadrants array before calculating its power set, so it won't be in the final result.)
If you do that, you may be able to optimize the last step by only calculating the subsets of length 4 of the longer quadrants; try using this as a guide for that if you need it: https://www.geeksforgeeks.org/print-subsets-given-size-set/

Get the minimum value from the array [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 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.

Categories