Find the number with the most digits in an array - javascript

Here we are. Stuck.
I've decided to create a function that finds the element with the most digits. If two of them have the same length, return the first one. Common sense tells us that it might be the highest number. Here is the code snippet:
function findLongest(array) {
var biggestNum = 0;
for(var i = 0; i < array.length; i++) {
if(array[i] > biggestNum) {
biggestNum = array[i];
}
}
return biggestNum;
}
findLongest([111,1111,5555,10000,1,90000]); //returns 90000 instead of 10000.
However, I can't meet the second condition (if length of two is the same, return the first one).
Any idea?

If you want digit-length comparing, cast the items into string and use length of them.
function findLongest(array) {
var biggestNum = 0;
for(var i = 0; i < array.length; i++) {
if(array[i].toString().length > biggestNum.toString().length) {
biggestNum = array[i];
}
}
return biggestNum;
}
console.log(findLongest([111, 1111, 5555, 10000, 1, 90000]));

You could take the integer value of the logarithm of 10 of the value for checking, because you get the count of digits (minus 1) for comapiring.
function findLongest(array) {
var biggestNum = array[0];
for (var i = 1; i < array.length; i++) {
if (Math.floor(Math.log10(array[i]) || 0) > Math.floor(Math.log10(biggestNum) || 0)) {
biggestNum = array[i];
}
}
return biggestNum;
}
console.log(findLongest([111, 1111, 5555, 10000, 1, 90000, 0]));

The second condition negates the first every time. The "highest number" right?
If you want the highest number why not sort and pop the last element as the returned value?
array[array.sort(function(a,b){return b - a}).length - 1]
Iteration of array is unnecessary

Related

How to stop random letter picking from doing duplicate letters [duplicate]

I need help with writing some code that will create a random number from an array of 12 numbers and print it 9 times without dupes. This has been tough for me to accomplish. Any ideas?
var nums = [1,2,3,4,5,6,7,8,9,10,11,12];
var gen_nums = [];
function in_array(array, el) {
for(var i = 0 ; i < array.length; i++)
if(array[i] == el) return true;
return false;
}
function get_rand(array) {
var rand = array[Math.floor(Math.random()*array.length)];
if(!in_array(gen_nums, rand)) {
gen_nums.push(rand);
return rand;
}
return get_rand(array);
}
for(var i = 0; i < 9; i++) {
console.log(get_rand(nums));
}
The most effective and efficient way to do this is to shuffle your numbers then print the first nine of them. Use a good shuffle algorithm.What Thilo suggested will give you poor results. See here.
Edit
Here's a brief Knuth Shuffle algorithm example:
void shuffle(vector<int> nums)
{
for (int i = nums.size()-1; i >= 0; i--)
{
// this line is really shorthand, but gets the point across, I hope.
swap(nums[i],nums[rand()%i]);
}
}
Try this once:
//Here o is the array;
var testArr = [6, 7, 12, 15, 17, 20, 21];
shuffle = function(o){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
shuffle(testArr);
This is relatively simple to do, the theory behind it is creating another array which keeps track of which elements of the array you have used.
var tempArray = new Array(12),i,r;
for (i=0;i<9;i++)
{
r = Math.floor(Math.random()*12); // Get a random index
if (tempArray[r] === undefined) // If the index hasn't been used yet
{
document.write(numberArray[r]); // Display it
tempArray[r] = true; // Flag it as have been used
}
else // Otherwise
{
i--; // Try again
}
}
Other methods include shuffling the array, removing used elements from the array, or moving used elements to the end of the array.
If I understand you correctly, you want to shuffle your array.
Loop a couple of times (length of array should do), and in every iteration, get two random array indexes and swap the two elements there. (Update: if you are really serious about this, this may not be the best algorithm).
You can then print the first nine array elements, which will be in random order and not repeat.
Here is a generic way of getting random numbers between min and max without duplicates:
function inArray(arr, el) {
for(var i = 0 ; i < arr.length; i++)
if(arr[i] == el) return true;
return false;
}
function getRandomIntNoDuplicates(min, max, DuplicateArr) {
var RandomInt = Math.floor(Math.random() * (max - min + 1)) + min;
if (DuplicateArr.length > (max-min) ) return false; // break endless recursion
if(!inArray(DuplicateArr, RandomInt)) {
DuplicateArr.push(RandomInt);
return RandomInt;
}
return getRandomIntNoDuplicates(min, max, DuplicateArr); //recurse
}
call with:
var duplicates =[];
for (var i = 1; i <= 6 ; i++) {
console.log(getRandomIntNoDuplicates(1,10,duplicates));
}
const nums = [1,2,3,4,5,6,7,8,9,10,11,12];
for(var i = 1 ; i < 10; i++){
result = nums[Math.floor(Math.random()*nums.length)];
const index = nums.indexOf(result);
nums.splice(index, 1);
console.log(i+' - '+result);
}

Running sum until a negative number

Please how can I use the index 0 in a for-loop when testing an empty array for a drumming sum.
Also, the single negative value returns zero
The sum should stop when a negative number is encountered
let sum = 0;
let lenArr = arr.length;
for (let i = 0; i <= lenArr - 1; i++) {
if (lenArr === 0) {
break;
}
if (arr[i] > 0) {
sum = sum + arr[i];
} else {
break;
}
}
return sum;
}
let input = [];
runningSum(input);
For an empty array the for loop would not be executed and it will return 0.
You can remove the if condition to check length of array inside for loop.

Can someone explain how this for loop works?

This function returns the largest number from an array. I need help on understanding the if part: if (arr[i] > maxNumber) {maxNumber = arr[i]}. Using pseudocode or an explanation how exactly does this work?
function max(arr){
let maxNumber = 0
for (i = 0; i < arr.length; i++) {
if (arr[i] > maxNumber){
maxNumber = arr[i]
}
}
return maxNumber
}
console.log(max([1,2,3,40,5]));
function max(array) {
largest number is equal to 0;
for (i is equal to 0; i is less than the length of array; increment i by 1 each time) {
if (the item at the current index is greater than the previous largest number) {
set the largest number to to the item at the current index;
}
}
return largest number which will be 0 if no larger number is found
}
Worth noting that if all values in the array are negative, it will return 0;
This will work for negative numbers too.
function max(arr){
let maxNumber = -Infinity
for (i = 0; i < arr.length; i++) {
if (arr[i] > maxNumber){
maxNumber = arr[i]
}
}
return maxNumber
}
What this part: if (arr[i] > maxNumber) {maxNumber = arr[i]} doing is checking if the current array member is greater than the current maxNumber and if it is changing the maxNumber value to the current array.
In short, max(whatEverArray) will always return zero or the biggest value of the array. and if it return zero, means all the array values are either zero or less than zero.

subtracting two integers which have different indexes but the same value

The given function should accept any array with any number of integers.
After it should subtract each two integers from each other beside integers which the same index.
var ArrayFirst = [4, 34, 6, 1,5];
var ArraySecond = [4,34,6,1,4];
function Find(Arg) {
var ResultArray;
var SecondArray = [];
for (var i = 0; i < Arg.length; i++) {
var FirsArray;
for (var j = 0; j < Arg.length; j++) {
if (Arg.indexOf(Arg[i]) != Arg.indexOf(Arg[j])) {
console.log(Arg.indexOf(Arg[i]) + 'AND' + Arg.indexOf(Arg[j]));
FirstArray = Arg[i] - Arg[j];
console.log(FirstArray);
if (FirstArray >= 0) {
SecondArray.push(FirstArray);
}
}
}
//console.log(SecondArray);
}
ResultArray = Math.min.apply(this, SecondArray);
console.log(ResultArray);
return ResultArray;
}
So After debuging I found that function Find works with ArrayFirst but not with ArraySecond
Question : Why function is not working when Array has two integers with the same value ?
UPDATED
Sorry guys that I haven't putted more details
Task: Create a function which as result will give the sum of subtracting two integers from an array, the sum cannot be subtraction of two integers which have the same index, the sum cannot be bellow 0, the sum closest to the 0 will be the proper result.
Example: [4,6,3,56,4]
4-6,4-3...4-4, then 6-4,6-3...6-4 then etc
So result will be 0 because 4-4 sum is 0
Some Hints
Firstly i thought maybe it is something to do with operation on array but then i used apply and call on arguments
if (Array.prototype.indexOf.apply(arguments[0],[arguments[0][i]]) !== Array.prototype.indexOf.apply(arguments[0],[arguments[0][j]])) {
FirstArray = arguments[0][i] - arguments[0][j];
if (FirstArray >= 0) {
SecondArray.push(FirstArray);
}
}
}
But is still not working, I tried the solution with to arguments, doesn't work either.
First and foremost avoid using Array as a variable name because for the most browser it will lead to unexpected behavior.
I think also you need to pass the two arrays in your function as parameters in order compare them.
And you don't need to find the index of element with index i, the i itself is an index.
Array.indexOf(Array[i]) === i
Here is an example of what you might need:
// it is important for the "array" value to start with lower case symbow, because JavaScript is case sensitive language and it already has object called "Array"
var array = [4, 6, 3, 56, 4];
function find(array)
{
// because we are surching for the minimum, the variable that will hold it needs to have the largest possible variable at the begining
var min = Number.MAX_VALUE;
for (var i = 0 ; i < array.length; i++)
{
for (var j=0 ; j < array.length; j++)
{
if (i != j)
{
var sum = array[i] - array[j];
// if it sum is greater than zero and it is the smallest sum so far we will save it in "min"
if (sum >= 0 && min > sum)
{
min = sum;
}
}
}
}
// retun the smallest sum we've encountered
return min;
}
console.log(find(array));

Variable amount of nested for loops

Edit: I'm sorry, but I forgot to mention that I'll need the values of the counter variables. So making one loop isn't a solution I'm afraid.
I'm not sure if this is possible at all, but I would like to do the following.
To a function, an array of numbers is passed. Each number is the upper limit of a for loop, for example, if the array is [2, 3, 5], the following code should be executed:
for(var a = 0; a < 2; a++) {
for(var b = 0; b < 3; b++) {
for(var c = 0; c < 5; c++) {
doSomething([a, b, c]);
}
}
}
So the amount of nested for loops is equal to the length of the array. Would there be any way to make this work? I was thinking of creating a piece of code which adds each for loop to a string, and then evaluates it through eval. I've read however that eval should not be one's first choice as it can have dangerous results too.
What technique might be appropriate here?
Recursion can solve this problem neatly:
function callManyTimes(maxIndices, func) {
doCallManyTimes(maxIndices, func, [], 0);
}
function doCallManyTimes(maxIndices, func, args, index) {
if (maxIndices.length == 0) {
func(args);
} else {
var rest = maxIndices.slice(1);
for (args[index] = 0; args[index] < maxIndices[0]; ++args[index]) {
doCallManyTimes(rest, func, args, index + 1);
}
}
}
Call it like this:
callManyTimes([2,3,5], doSomething);
Recursion is overkill here. You can use generators:
function* allPossibleCombinations(lengths) {
const n = lengths.length;
let indices = [];
for (let i = n; --i >= 0;) {
if (lengths[i] === 0) { return; }
if (lengths[i] !== (lengths[i] & 0x7fffffff)) { throw new Error(); }
indices[i] = 0;
}
while (true) {
yield indices;
// Increment indices.
++indices[n - 1];
for (let j = n; --j >= 0 && indices[j] === lengths[j];) {
if (j === 0) { return; }
indices[j] = 0;
++indices[j - 1];
}
}
}
for ([a, b, c] of allPossibleCombinations([3, 2, 2])) {
console.log(`${a}, ${b}, ${c}`);
}
The intuition here is that we keep a list of indices that are always less than the corresponding length.
The second loop handles carry. As when incrementing a decimal number 199, we go to (1, 9, 10), and then carry to get (1, 10, 0) and finally (2, 0, 0). If we don't have enough digits to carry into, we're done.
Set up an array of counters with the same length as the limit array. Use a single loop, and increment the last item in each iteration. When it reaches it's limit you restart it and increment the next item.
function loop(limits) {
var cnt = new Array(limits.length);
for (var i = 0; i < cnt.length; i++) cnt[i] = 0;
var pos;
do {
doSomething(cnt);
pos = cnt.length - 1;
cnt[pos]++;
while (pos >= 0 && cnt[pos] >= limits[pos]) {
cnt[pos] = 0;
pos--;
if (pos >= 0) cnt[pos]++;
}
} while (pos >= 0);
}
One solution that works without getting complicated programatically would be to take the integers and multiply them all. Since you're only nesting the ifs, and only the innermost one has functionality, this should work:
var product = 0;
for(var i = 0; i < array.length; i++){
product *= array[i];
}
for(var i = 0; i < product; i++){
doSomething();
}
Alternatively:
for(var i = 0; i < array.length; i++){
for(var j = 0; j < array[i]; j++){
doSomething();
}
}
Instead of thinking in terms of nested for loops, think about recursive function invocations. To do your iteration, you'd make the following decision (pseudo code):
if the list of counters is empty
then "doSomething()"
else
for (counter = 0 to first counter limit in the list)
recurse with the tail of the list
That might look something like this:
function forEachCounter(counters, fn) {
function impl(counters, curCount) {
if (counters.length === 0)
fn(curCount);
else {
var limit = counters[0];
curCount.push(0);
for (var i = 0; i < limit; ++i) {
curCount[curCount.length - 1] = i;
impl(counters.slice(1), curCount);
}
curCount.length--;
}
}
impl(counters, []);
}
You'd call the function with an argument that's your list of count limits, and an argument that's your function to execute for each effective count array (the "doSomething" part). The main function above does all the real work in an inner function. In that inner function, the first argument is the counter limit list, which will be "whittled down" as the function is called recursively. The second argument is used to hold the current set of counter values, so that "doSomething" can know that it's on an iteration corresponding to a particular list of actual counts.
Calling the function would look like this:
forEachCounter([4, 2, 5], function(c) { /* something */ });
This is my attempt at simplifying the non-recursive solution by Mike Samuel. I also add the ability to set a range (not just maximum) for every integer argument.
function everyPermutation(args, fn) {
var indices = args.map(a => a.min);
for (var j = args.length; j >= 0;) {
fn.apply(null, indices);
// go through indices from right to left setting them to 0
for (j = args.length; j--;) {
// until we find the last index not at max which we increment
if (indices[j] < args[j].max) {
++indices[j];
break;
}
indices[j] = args[j].min;
}
}
}
everyPermutation([
{min:4, max:6},
{min:2, max:3},
{min:0, max:1}
], function(a, b, c) {
console.log(a + ',' + b + ',' + c);
});
There's no difference between doing three loops of 2, 3, 5, and one loop of 30 (2*3*5).
function doLots (howMany, what) {
var amount = 0;
// Aggregate amount
for (var i=0; i<howMany.length;i++) {
amount *= howMany[i];
};
// Execute that many times.
while(i--) {
what();
};
}
Use:
doLots([2,3,5], doSomething);
You can use the greedy algorithm to enumerate all elements of the cartesian product 0:2 x 0:3 x 0:5. This algorithm is performed by my function greedy_backward below. I am not an expert in Javascript and maybe this function could be improved.
function greedy_backward(sizes, n) {
for (var G = [1], i = 0; i<sizes.length; i++) G[i+1] = G[i] * sizes[i];
if (n>=_.last(G)) throw new Error("n must be <" + _.last(G));
for (i = 0; i<sizes.length; i++) if (sizes[i]!=parseInt(sizes[i]) || sizes[i]<1){ throw new Error("sizes must be a vector of integers be >1"); };
for (var epsilon=[], i=0; i < sizes.length; i++) epsilon[i]=0;
while(n > 0){
var k = _.findIndex(G, function(x){ return n < x; }) - 1;
var e = (n/G[k])>>0;
epsilon[k] = e;
n = n-e*G[k];
}
return epsilon;
}
It enumerates the elements of the Cartesian product in the anti-lexicographic order (you will see the full enumeration in the doSomething example):
~ var sizes = [2, 3, 5];
~ greedy_backward(sizes,0);
0,0,0
~ greedy_backward(sizes,1);
1,0,0
~ greedy_backward(sizes,2);
0,1,0
~ greedy_backward(sizes,3);
1,1,0
~ greedy_backward(sizes,4);
0,2,0
~ greedy_backward(sizes,5);
1,2,0
This is a generalization of the binary representation (the case when sizes=[2,2,2,...]).
Example:
~ function doSomething(v){
for (var message = v[0], i = 1; i<v.length; i++) message = message + '-' + v[i].toString();
console.log(message);
}
~ doSomething(["a","b","c"])
a-b-c
~ for (var max = [1], i = 0; i<sizes.length; i++) max = max * sizes[i];
30
~ for(i=0; i<max; i++){
doSomething(greedy_backward(sizes,i));
}
0-0-0
1-0-0
0-1-0
1-1-0
0-2-0
1-2-0
0-0-1
1-0-1
0-1-1
1-1-1
0-2-1
1-2-1
0-0-2
1-0-2
0-1-2
1-1-2
0-2-2
1-2-2
0-0-3
1-0-3
0-1-3
1-1-3
0-2-3
1-2-3
0-0-4
1-0-4
0-1-4
1-1-4
0-2-4
1-2-4
If needed, the reverse operation is simple:
function greedy_forward(sizes, epsilon) {
if (sizes.length!=epsilon.length) throw new Error("sizes and epsilon must have the same length");
for (i = 0; i<sizes.length; i++) if (epsilon[i] <0 || epsilon[i] >= sizes[i]){ throw new Error("condition `0 <= epsilon[i] < sizes[i]` not fulfilled for all i"); };
for (var G = [1], i = 0; i<sizes.length-1; i++) G[i+1] = G[i] * sizes[i];
for (var n = 0, i = 0; i<sizes.length; i++) n += G[i] * epsilon[i];
return n;
}
Example :
~ epsilon = greedy_backward(sizes, 29)
1,2,4
~ greedy_forward(sizes, epsilon)
29
One could also use a generator for that:
function loop(...times) {
function* looper(times, prev = []) {
if(!times.length) {
yield prev;
return;
}
const [max, ...rest] = times;
for(let current = 0; current < max; current++) {
yield* looper(rest, [...prev, current]);
}
}
return looper(times);
}
That can then be used as:
for(const [j, k, l, m] of loop(1, 2, 3, 4)) {
//...
}

Categories