javascript convert hyphenated range to number
function getNumbers(stringNumbers) {
//personal preference, but I got this handy tip from the internet that
//if you had assignments, better if they are individually var'ed
var nums = [];
var entries = stringNumbers.split(',');
var length = entries.length;
//for variabes that don't, comma separated
var i, entry, low, high, range;
for (i = 0; i < length; i++) {
entry = entries[i];
//shortcut for testing a -1
if (!~entry.indexOf('-')) {
//absence of dash, must be a number
//force to a number using +
nums.push(+entry);
} else {
//presence of dash, must be range
range = entry.split('-');
//force to numbers
low = +range[0];
high = +range[1];
//XOR swap, no need for an additional variable. still 3 steps though
//http://en.wikipedia.org/wiki/XOR_swap_algorithm
if(high < low){
low = low ^ high;
high = low ^ high;
low = low ^ high;
}
//push for every number starting from low
while (low <= high) {
nums.push(low++);
}
}
}
//edit to sort list at the end
return nums.sort(function (a, b) {
return a - b;
});
}
Also in JavaScript:
- how to remove last index of array in javascript
- inline if else javascript
- use history in react router
- scroll to element in scrollable div
- javascript sort chars in string
- angular prod apache config for route
- clear cookies js
- discord js clear message from id
- jquery if is visible
- i18n react meta description
- how to iterate through a js object
- send message to specific channel discord.js
- javascript on script loaded
- jquery change selected option to first
- socket io query
- js check if variable is string
- jwt strategy
- jhow to make a fish in javascript
- bq show pretty json
- store data in array jquery
- module not found: can't resolve 'react' in 'c:\users\pavlos\desktop\reactjs\hwr\reactproject2\src'
- jQuery check element has focus
- function inside function javascript
- ready function jquery