checking if input is greater or lesser than array values - javascript

Sorry if this is a duplicate.
Let's say I have an array of 12 numbers, and I want to check those values against an input and then output how many values inside the array are higher than the input and lower than the input.
I wasn't sure how to do this in jQuery but if it's not possible in jQuery I am open to other suggestions. I wasn't sure if using the inArray or grep function would even work for this type of implementation.

Using jQuery you can run a foreach loop and count how many values are higher and lower from the array you have.
Try this:
var higher = 0;
var lower = 0;
$.each(arr, function(k,v){
// will miss if value is equals.
if (input < v) higher ++;
if (input > v) lower ++;
})
console.log("Higher values are: "+higher+ " | Lower values are: "+lower);
Assuming arr is your array and input variable is what user inputs.
ref: http://api.jquery.com/jquery.each/

Related

Sorting a dynamically filled array of objects

I have an array that is initialized like such var generationObject = [{string:"", score: 0}];
which I then fill dynamically:
for(var i = 0; i < amount_offspring; i++)
{
// "load" text into array and send the string to see if it evolves
generationObject[i].string = evolve(start_text, characters, mutation_rate);
// then score the string
generationObject[i].score = score(target_text, generationObject.string);
}
I then want to sort this array by score. I don't know what's best, to sort it in the for loop or sort the entire array afterwards.
I will then take the string of the highest scoring object and pass it through the function again, recursively.
So what would be a good way to go about this sort function? I've seen some here use this
generationObject.sort(function(a, b) {
return (a.score) - (b.score);
});
But I'm not sure if .sort is still supported? This didnt seem to work for me though.
generationObject is an array, not an object, so score(target_text, generationObject.string); could be the problem, as .string will be undefined. (Did you mean generationObject[i].string?)
Try building your array like this:
var generationObject = []
for(var i = 0; i < amount_offspring; i++)
{
evolved_string = evolve(start_text, characters, mutation_rate)
generationObject.push({
string: evolved_string,
score: score(target_text, evolved_string)
})
}
And then Array.prototype.sort should do the trick.
You should write your sorting logic outside the for loop, since if you put it inside, the object array will be sorted N times, where N being the iterations of your loop. The following are two ways to do it-
By using sort() function- To clarify your question, sort() is still supported across almost all the browsers. If you are still concerned about the browser compatibility, you can check the MDN documentation to see the list of supported browsers.
generationObject = generationObject.sort(function(a, b) {
return parseInt(a.score) - parseInt(b.score);
});
By using underscorejs-
In underscore, you can take advantage of the sortBy() function.
Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee. iteratee may also be the string name of the property to sort by (eg. length).
You can simply do this in underscorejs-
generationObject = _.sortBy(generationObj, 'score');

Angularjs Synced inputs for percetange

I need to have x amount of text input fields.
Each input field will start off with an equal value to the rest.
This value is 100 / Number of inputs.
I need to have the input fields synced, so when one value is changed, the rest of the values are recalculated to equal 100.
This problem seems fairly complex to solve, but I'm sure it must have been done before, it seems like it could be quite common.
Instead of me badly explaining the problem, here is an example of it working. It doesn't use text fields, rather sliders. But the solution is the same.
https://www.humblebundle.com/weekly
Click Choose where your money goes
jsFiddle for example: http://jsfiddle.net/r4qbx9Lb/5/
First of all, you need a $watch in your controller since you want all your input values to change based on a change in any of them. In your $watch callback function, you need to figure out which array index (what input) had been changed by the user.
All of this would look something like the following:
// 100 in this example, but can be whatever
$scope.totalValue = 100;
$scope.$watch('inputs', function(newValue, oldValue){
for(var i = 0; i < newValue.length; i++){
if(newValue[i].value != oldValue[i].value){
$scope.changedIndex = i;
$scope.leftOverValue = $scope.totalValue - newValue[i].value;
$scope.percentChange = (newValue[i].value - oldValue[i].value) / oldValue[i].value;
console.log($scope.leftOverValue);
console.log(percentChange);
}
}
}, true);
Once you have this information, you can apply it to all of the array values that weren't changed by the user. So run another loop (you'll need to split out your two loops because you don't know what index the changed value is initially) and change any of the non-affected index values in this loop:
for(var x = 0; x < newValue.length; x++){
if($scope.changedIndex != x){
//Do math and apply logic
}
}
Then you can apply any math formula you want to adjust your other values. You'll have to figure this part out - depending on if you want your other values to change proportionally to their old value or proportionally to the change the user made to the input they edited.
http://jsfiddle.net/r4qbx9Lb/10/

Can someone explain to me why this loop doesn't give me the desired result?

Basically, I'm writing a little algorithm that takes in a random array with only numbers and spits out the 2nd highest number in the array (assuming all entries in the array are numbers and there are at least 2 entries). Here is the code:
var secondGreatest = function(numberArray){
var array = numberArray;
var result = [];
for(var i = 0; i < array.length; i++){
if(array[i] === Math.max.apply(Math, array)){
result.push(array[i]);
array.splice(i, 1);
}
};
return result[1];
};
So what I'm doing is setting the input array of numbers to variable "array". Then I set variable "result" to an empty array.
In the for loop, I specify that if the array at the ith position equals the highest number of the array, push that number into the empty array and remove that number from the original array. Since the "result" array will have the order from highest to lowest number, I call result[1] to give me the 2nd highest number.
However, the result array only contains one entry and it's the highest number of the previous array. After that, the for loop seems to stop. I tried the "continue;" statement, but nothing works.
Any help as to why this doesn't work is appreciated.
Here is a shorter code, if you want to keep using the Math.max method.
var secondGreatest = function(numberArray){
var top1 = Math.max.apply(Math, numberArray);
numberArray.splice(numberArray.indexOf(top1));
return Math.max.apply(Math, numberArray);
};
var arr = [1,3,5,7,4,2];
alert(secondGreatest(arr));
You don't really need to iterate, and actually iterating would make it necessary to reset the for whenever you remove the max item. Tushar's answer in the comment is more compact though, and problably makes more sense.
When you do a splice, the array is being re-indexed and array.length which was cached becomes obsolete. This is why the for loop stops. You can start at the end and iterate backwards to fix this.

property undefined but console.log can see it

I am creating a dynamic form which consists of sku, qty and subtotal. So the form starts out from no inputs and filled up as the user searches for products. Upon keypress I need to compute the total quantity and show it to the user via javascript.
I am using getting all quantity input elements via getElementsByClassName upon keypress and counting all values. however the browser is returning an error (Uncaught TypeError: Cannot read property 'value' of undefined) but I can log the value.
I only use native JavaScript no jQuery.
qtyelements = document.getElementsByClassName('qty-element');
var x;
var count = 0;
for(x = 0; x <= qtyelements.length; x++){
console.log(qtyelements[x].value);
count = count + parseInt(qtyelements[x].value);
}
Here is an image of my
problem
Length gives you the number of items, but the collection is zero based. So for example while the length may be three, you only loop to two (0, 1, 2).
So change:
x <= qtyelements.length
to:
x < qtyelements.length
In your case it looks like you only have two elements, so the indices would be 0 and 1, but you check to see if 2 exists with the <=, hence the undefined.

Checking if element exists in array without iterating through it

my array:
tempListArray = "[{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}]";
To check if an element exists I would do this:
for (var i in tempListArray) {
//check flag
if (tempListArray[i].id == Id) {
flagExistsLoop = 1;
break;
}
}
Is there anyway, I can check if an Id exists without looping through the whole array. Basically I am worried about performance if say I have a 100 elements.
Thanks
No, without using custom dictionary objects (which you seriously don't want to for this) there's no faster way than doing a 'full scan' of all contained objects.
As a general rule of thumb, don't worry about performance in any language or any situation until the total number of iterations hits 5 digits, most often 6 or 7. Scanning a table of 100 elements should be a few milliseconds at worst. Worrying about performance impact before you have noticed performance impact is one of the worst kinds of premature optimization.
No, you can't know that without iterating the array.
However, note for...in loops are a bad way of iterating arrays:
There is no warranty that it will iterate the array with order
It will also iterate (enumerable) non-numeric own properties
It will also iterate (enumerable) properties that come from the prototype, i.e., defined in Array.prototype and Object.protoype.
I would use one of these:
for loop with a numeric index:
for (var i=0; i<tempListArray.length; ++i) {
if (tempListArray[i].id == Id) {
flagExistsLoop = 1;
break;
}
}
Array.prototype.some (EcmaScript 5):
var flagExistsLoop = tempListArray.some(function(item) {
return item.id == Id;
});
Note it may be slower than the other ones because it calls a function at each step.
for...of loop (EcmaScript 6):
for (var item of tempListArray) {
if (item.id == Id) {
flagExistsLoop = 1;
break;
}
}
Depending on your scenario, you may be able to use Array.indexOf() which will return -1 if the item is not present.
Granted it is probably iterating behind the scenes, but the code is much cleaner. Also note how object comparisons are done in javascript, where two objects are not equal even though their values may be equal. See below:
var tempListArray = [{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}];
var check1 = tempListArray[2];
var check2 = {"id":"9","value":false};
doCheck(tempListArray, check1);
doCheck(tempListArray, check2);
function doCheck(array, item) {
var index = array.indexOf(item);
if (index === -1)
document.write("not in array<br/>");
else
document.write("exists at index " + index + "<br/>");
}
try to use php.js it may help while you can use same php function names and it has some useful functionalities
There is no way without iterating through the elements (that would be magic).
But, you could consider using an object instead of an array. The object would use the (presumably unique) id value as the key, and the value could have the same structure you have now (or without the redundant id property). This way, you can efficiently determine if the id already exists.
There is a possible cheat for limited cases :) and it is magic...cough cough (math)
imagine you have 3 elements:
1
2
3
and you want to know if one of these is in an array without iterating it...
we could make a number that contains a numerical flavor of the array. we do this by assigning prime numbers to the elements:
1 - 2
2 - 3
3 - 5
the array so when we add item 2 we check that the array doesn't already contain the prime associated to that item by checking (if Flavor!=0 && (Flavor%3)!=0) then adding the prime Flavor*=3;
now we can tell that the second element is in the array by looking at the number.
if Flavor!=0 && (Flavor%3)==0 // its There!
Of course this is limited to the numerical representation that can be handled by the computer. and for small array sizes (1-3 elements) it might still be faster to scan. but it's just one idea.
but the basis is pretty sound. However, this method becomes unusable if you cannot correlate elements one to one with a set of primes. You'll want to have the primes calculated in advance. and verify that the product of those is less numerical max numerical representation. (also be careful with floating-point. because they might not be able to represent the number at the higher values due to the gaps between representable values.) You probably have the best luck with an unsigned integer type.
This method will probably be too limiting. And there is something else you can do to possibly speed up your system if you don't want to iterate the entire array.
Use different structures:
dictionaries/maps/trees etc.
if your attached to the array another method can be a bloom filter. This will let you know if an element is not in your set, which can be just as useful.

Categories