JQuery Slider Weird Values - javascript

My JQuery slider's value getter is not working.
var value = $("#slider").slider("option", "value")
It does not return the right value. My slider's range is from 1 to 5, if that helps. It returns odd values that do not correspond to the actual value. If I move it to the end and back again, instead of going 1, 2, 3, 4, 5, 4, 3, 2, 1, like it should, it does 0, 0, 2, 3, 4, 5, 4, 3, 2. The 5 isn't even at the end, when I get to the end it says 4 but when I slide it one back it says 5!

When are you trying to query these values?
If it's during the slide callback you'll get the previous value, not the current, and you should use the ui.value parameter instead.

Related

How to splice multiple array elements and insert accordingly?

const data = response.data
console.log(data)
const temp = data.ssps_with_scated.splice(5, 1)(1, 3)[0]
data.ps_with_ed.splice(2, 1, 0, temp)
i am trying to achieve finally i got it. But issue is, i cant expect the array value same all the time. So i have decided to re-arrange the array values based on the ID.
Well,
splice(7,1)(21,3)
This code will cause an error. Since Array.prototpy.slice returns a new array.
It would be the same if you would do this:
const a = [1,2,3]
const b = a.splice(1,1);
b(2,1) // b.splice(...) is not a function
EDITED:
Maybe there is a faster/better solution but...
You can make it more general but for your case only:
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
const first = array[7];
const second = array[21];
// Add elements on certain indices in array (second and third)
array.splice(2, 0, first, second)
// Remove first from the array (index is 7 + 2 because we added 2 elements)
array.splice(9, 1)
// Remove 21 from the array (index is 22 - 1 because we added 2 elements and removed 1, not: number 21 is on index 22)
array.splice(21, 1);
data shouldn't be a const since the value is getting updated. Splice can also only be called on one array at a time. If you need to call it on multiple arrays, create a loop or iterate over them.
If you want to inject the current value of the 7th position into the 2nd position... you'd need to do something like...
array.splice(2, 0, array[7])

find unpaired element in array - how does this work?

There was a question on how to find the unpaired element in an array. So if an element occurs in an array 3 times, or 5 times, or any unpaired times.
The fastest method (it works ok!) was this:
function pairing (A) {
var s = new Set;
A.forEach(function(v) {
s.delete(v) || s.add(v)
});
return s.values().next().value;
}
const arr = [1, 2, 5, 1, 2, 5, 7, 8, 7, 7, 8, 7, 1, 1, 3];
console.log(pairing(arr));
It will give 3 as a result, that's the unpaired element.
The solution works, but I don't know what it does. Please somebody give me an explanation for this!
What is this line:
s.delete(v) || s.add(v)
I can't interpret it.
And, this one:
s.values().next().value;
?
Please detail what is happening here.
Thank you !
The logical or operator (||) short-circuits and returns the first value that is not false (or falsey, but that is not relevant here).
Set#delete returns true if the element was deleted, i.e. it did originally exist in the set, and false otherwise. Set#add returns true if the element was added, i.e. it did not already exist in the set, and false otherwise.
Thus, s.delete(v) || s.add(v) is really saying delete v if it already exists in the set; otherwise, add it. This works due to the short-circuiting of the logical or operator: if set.delete(v) returns true, s.add(v) will never be executed.
Set#values returns the iterator for the values of the set in the order they were inserted. .next() gets the next element from the iterator and .value gets the value of that element. In this case, it returns the first and only element of the Set, which is the unpaired element.
On another note, the standard and most efficient way to solve this problem for integers is to apply the bitwise XOR (^) operator. Since a ^ a === 0 for all integers, simply XORing all the numbers in the array will find the number that appeared an odd amount of times.
const arr = [1, 2, 5, 1, 2, 5, 7, 8, 7, 7, 8, 7, 1, 1, 3];
function unpaired(arr){
return arr.reduce((acc,curr)=>acc^curr,0);
}
console.log(unpaired(arr));
s.delete(v) || s.add(v) - this delete a value if present in a set or add if not already present in a set "s".
s.values().next().value; - this one return the value left in the set(unpaired one)
As per the code -
function pairing (A) {
var s = new Set;
A.forEach(function(v) {
console.log(s)
s.delete(v) || s.add(v)
});
return s.values().next().value;
}
const arr = [1, 2, 5, 1, 2, 5, 7, 8, 7, 7, 8, 7, 1, 1, 3];
console.log(pairing(arr));
}
const arr = [1, 2, 5, 1, 2, 5, 7, 8, 7, 7, 8, 7, 1, 1, 3];
console.log(pairing(arr));
The output is
Simplified version of the function you posted. Hope it is understandable now
var set = new Set();
const arr = [1, 2, 5, 1, 2, 5, 7, 8, 7, 7, 8, 7, 1, 1, 3];
arr.forEach(ele => {
if(set.has(ele)){
set.delete(ele);
} else {
set.add(ele);
}
})
console.log(set.values().next().value);
// This gives the element left over in set which is unpaired.

How to edit variable from array instead of increments?

Follow this for full code: http://codepen.io/anon/pen/JWRabY
Here is the part im having trouble with:
var dataID = 1;
$('svg rect.grid').each(function() {
$(this).attr('data-id', dataID);
dataID++
});
This dynamically creates an attribute 'data-id' to each 'svg rect.grid'(ie. a square) created starting from 1. The code generates 9 Squares.
I would like to change the dataID to use an array such as
4, 9, 2, 3, 5, 7, 8, 1, 6
Instead of sequentially numbering each square 1-9.
I hope this makes sense.
Just use an array and shift to get each element in turn:
var ids = [4, 9, 2, 3, 5, 7, 8, 1, 6];
$('svg rect.grid').each(function() {
$(this).attr('data-id', ids.shift());
});
Note that this alters the array, so if you need to run this process multiple times, ensure that you are not storing the array somewhere permanently and that you re-initialize it each time. (Putting this code inside of a function and calling it, for example, would work.)

Finding the lowest number in an array using split('') and reverse();

I am trying to find the min value of an array, and am trying to do it by sorting the array, and then reversing the array, and then calling the very first index of the array.
Unfortunately with what I have been trying, I keep getting 9. (don't know why) Can anybody take a quick look at what I have been doing and bail me out here? (i'm using js)
var minny = [4, 3, 5, 2, 6, 3, 4, 5, 2, 3, 4, 6, 7, 8, 9, 9, 1, 11, 25];
var smallest = function (minny){
minny = minny.sort('');
var sorted = minny + " ";
sorted = minny.reverse('').join('');
return sorted[0];
}
console.log(smallest(minny))
By default the sort method sorts elements alphabetically(11 comes before 9) and therefore you need to add a compare function as a param.
var smallest = function (minny) {
minny = minny.sort(function(a, b) { return a - b; });
return minny[0];
}
console.log(smallest(minny))
JSFIDDLE.
Based on your code, you could just do
return minny.sort()[0];
So, your full code example becomes
var minny = [4, 3, 5, 2, 6, 3, 4, 5, 2, 3, 4, 6, 7, 8, 9, 9, 1, 11, 25];
var smallest = function (minny){
return minny.sort()[0];
}
console.log(smallest(minny))
You're calling minny.sort('') which is using the default natural sort, so 11 and 25 end up near the beginning because of the 1 and 2.
What you have to do is call sort with a function that compares numbers, such as:
minny.sort(function(a,b) { return b-a; });
This will sort minny the way you want it.
There is no need to even call reverse and join afterwards, just return the first item. "return sorted[0]" is fine but will fail if there are no items, so you might just want to call "return sorted.shift()" instead. This will return the first item too, but won't fail if the array is empty.
PS. your call to minny.reverse also has an empty string as a parameter. That's not needed, reverse takes no parameters.
sort() sorts alphabetically by string representation, so in your case it would result in 1, 11, 2, 2, 25, .... You have to provide a comparison function for correct integer sorting, although in your specific case it doesn't really make a difference.
var smallest = function (minny){
minny = minny.sort(function(a, b){return a-b});
return minny[0];
}
See jsfiddle
Using sort is fairly short code to write, and it will return the correct number if you use minny.sort(function(a,b){return a-b})[0].
If you have a large unordered array you are running the comparison many times and you are sorting the array, which is not usually what you want to do to an array.
It may be better to just iterate the members and compare each just once to the lowest fond so far.
var minny= [4, 3, 5, 2, 6, 3, 4, 5, 2, 3, 4, 6, 7, 8, 9, 9, 1, 11, 25];
var smallest= function(minny){
var min= Infinity;
minny.forEach(function(next){
if(next<min) min= next;
});
return min;
}
Or use Math.min, if this is code golf:
Math.min.apply(Array,minny);

Array.prototype.sort Temporarily Duplicating Content?

I recently wanted to walk somebody through how Array.prototype.sort uses a custom method to compare two values at any given time, and decide whether they should be swapped or left alone. I decided to log the array during each comparison so the result of the previous comparison could be seen. When I logged the array, I noticed something rather odd about the state of the array at certain moments.
Assuming the following:
var num = [ 2, 1, 8, 5, 3 ];
num.sort( comparator );
function comparator ( a, b ) {
console.log( num ); // Current state of num
return a - b; // Order values numerically
}
This is the output:
[ 2, 1, 8, 5, 3 ] // Comparing 2 and 1
[ 1, 2, 8, 5, 3 ] // Comparing 2 and 8
[ 1, 2, 8, 5, 3 ] // Comparing 8 and 5
[ 1, 2, 8, 8, 3 ] // Comparing 2 and 5
[ 1, 2, 5, 8, 3 ] // Comparing 8 and 3
[ 1, 2, 5, 8, 8 ] // Comparing 5 and 3
[ 1, 2, 5, 5, 8 ] // Comparing 2 and 3
The array is sorted properly ([ 1, 2, 3, 5, 8 ]) but I am still left scratching my head at some of the passes on the collection itself.
How is it that 8 appears twice on iteration 4, replacing 5 temporarily. And again, 8 appears twice two iterations later replacing 3 temporarily. Lastly, 5 appears twice, temporarily replacing 3 in the last iteration.
Note that the above code was ran in Chrome.
Interesting, but not too surprising.
It appears to be using a simple insertion-sort algorithm in this instance.
Something along the lines of:
Get item [1]
Shift every item below it up one until you find an item that's lower, then put above this item
Get item [2]
Shift every item below it up one until you find an item that's lower, then put it above this item
Get item [3]
(cont)
When describing the bubble sort algorithm, you usually imagine each element being swapped along the array until it finds its place. But it's more efficient to store the item into a temporary variable than to swap it.

Categories

Resources