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 1 year ago.
Improve this question
For example, I have here the data in the database
id range price
1-3 30
4-8 50
9-13 80
14-20 120
21-29 160
I want that when the user inputs any number the price will display depending on the ranged.
For example, the user input number 5, since 5 belongs to the range of 4-8 the 80 will display as price. another example is when the user inputs number 23 the 160 will display as price since 23 belongs to the range of 21-29.
Using Javascript:
You could build an array of objects with the price and range as keys where the price is the key of the prices value and range is an array of constraints for the start and finish of that section.
Then iterate over the possible objects that exist in the array and see if the ID falls within the constraints using a conditional that checks if the low number of the range, range[0] is less than or equal to the ID AND the high number of the range, range[1] is greater than or equal to the ID. When you get a match use range.price to get the price value within the range.
const userInput = document.querySelector("#userInput");
const checkPrice = document.querySelector("#checkPrice");
const price = document.querySelector("#price");
const ranges = [{
price: 30,
range: [1, 3]
},
{
price: 50,
range: [4, 8]
},
{
price: 80,
range: [9, 13]
},
{
price: 120,
range: [14, 20]
},
{
price: 160,
range: [21, 29]
}
]
function getIdRangesPrice(e) {
const id = userInput.value;
ranges.forEach((range, i) => range.range[0] <= id && range.range[1] ? price.textContent = `$${range.price}` : null)
}
checkPrice.addEventListener('click', getIdRangesPrice)
/*If you want to get the next price level do the following
ranges.forEach((range, i) => range.range[0] <= id && range.range[1] >= id && ranges[i + 1] !== undefined ? price.textContent = `$${Object.values(ranges[i + 1])[0]}` : null)
*/
<input id="userInput" min="1" max="29" type="number"><button id="checkPrice">Check Price</button>
<div id="price"></div>
Assuming you have database with column start and end
with 1 as start and 3 as end
you can run query select price from rangeTable where $userInput between start and end
Related
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 2 days ago.
Improve this question
I really don know how to title this question but here is what i am wondering.
I have this array of numbers:
numbers = [5, 10, 20, 25, 30, 40]
I want to remove numbers that dont increase by 10. So what i mean by that, inn a for loop, if the current index + 10 is not inn the array, then i want to delete that number. so the correct number array would be.
filteredNumbers = [10, 20, 30, 40]
I hope this makes sense. kinda har to explain exactly with words.
You could check pairs with delta of 10.
const
numbers = [5, 10, 20, 25, 30, 40],
result = numbers.filter((v, i, { [i - 1]: prev, [i + 1]: next }) =>
prev + 10 === v || v + 10 === next
);
console.log(result);
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 1 year ago.
Improve this question
Receives an integer array as argument
•
The function transverses the array to determine the minimum and
maximum values in the array
Displays the calculated information as illustrated below:
functionName([-8, -1, -87, -14, -81, -74, -20, -86, -61, -10]);
// would produce following message in console:
The minimum value in the array is: -87, the maximum value is -1
Math.min and Math.max return the minimum and maximum values. Since you want your function to print it out to the console, use console.log to print out these values, along with a templated string to have it in the format you want.
const minAndMax = (arr) => console.log(`The minimum value in the array is: ${Math.min(...arr)}, the maximum value is ${Math.max(...arr)}`)
minAndMax([-8, -1, -87, -14, -81, -74, -20, -86, -61, -10]);
this will work.
function yourFunc(arr){
arr = arr.sort( (a,b) => a -b );
console.info(`The minimum value in the array is: ${arr[0]}, the maximum value is ${arr[arr.length - 1]}`);
}
yourFunc([-8, -1, -87, -14, -81, -74, -20, -86, -61, -10])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to clear multiple currencies (like the US and EU) and round up. What I'm trying to do is;
10.30 → 10
10.60 → 11
849.95 → 850
1,022.20 → 1022
1.022,20 → 1022
230,20 → 230
30,20 → 30
I can do it if it's separated by a dot but the comma ones don't work https://jsfiddle.net/pmhgx7ut/
Building on what you already wrote, you can try the following:
var price = "1.022,20";
// comma and dot case
if (price.indexOf(',') < price.indexOf('.')) {
var price = price.replace(/[^0-9\.\,]/g, '');
var price = price.replace(",","");
};
// comma case
if (price.includes(",")==true && price.includes(".")==false)
{var price = price.replace(",",".");}
else //dot and comma case
{
if (price.indexOf('.') < price.indexOf(',')) {
var price = price.replace(",",".");
var price = price.replace(".",",");
var price = price.replace(/[^0-9\.\,]/g, '');
var price = price.replace(",","");};
};
var price = Math.round(price);
alert(price);
You should be able to do something like this:
prices = [
'£6,000.98',
'£6.000,98',
'£600.30',
'£600,30',
'£200',
];
prices.forEach(price => {
// Check for a dot or comma followed by 2 digits at the end of price
const cents_match = price.match(/[\,\.](\d{2})$/);
// Check if we got a match and round the calue, or set cents to 0
const cents = cents_match ? Math.round(cents_match[1] / 100) : 0;
// Get the price without cents, add the rounded cents value.
price = +(cents_match ? price.slice(0, price.length - 3) : price).replace(/\D/g, '') + cents;
// Output
console.log(price);
});
I am trying to work out the best way to achieve the following:
A score might require a total of 25 'items' AS A MINIMUM
currently that person might have 15.8 'items'
I have a number of items required to reach that score (9.2)
So to get that score the minimum a person must have is 10 'items' in x weeks (to be 25.8 and over the 25 threshold).
Assuming they have 4 weeks to do it that gives 2.5 needed per week.
What I am struggling with is how to output an array of 'whole items' that will make 10.
e.g.
2
3
2
3
I want the items to be as evenly distributed as possible.
so
1
2
3
4
would be useless
I am just trying to work out the best way to do this.
I was thinking of:
finding nearest whole number ROUNDED UP (3 in this example)
Then outputting that number
Then on the next pass gathering the 'remainder' (-0.5) and then rounding the number.
But it doesn't quite work for all cases.
Can anyone help me get my head around it without writing hundreds of lines of code.
As a further example say 17 were needed in 5 weeks that would be
4
3
3
4
3
Thanks in advance.
You could do it some way like this:
function myFunction(total, weeks) {
var n = 0;
var temp = 0;
var arr = [];
while (n < total) {
temp = n;
n += total / weeks;
arr.push(Math.floor(n) - Math.floor(temp));
}
return arr;
}
myFunction(10, 4); //[2, 3, 2, 3]
myFunction(17, 5); //[3, 3, 4, 3, 4]
In this code, total / weeks will be the average number you'll want to add to n to get from 0 to total in exactly weeks iterations.
Now the difference between the original value of n (which is stored in temp) and the new value of n is pushed to the array. That will give you the rounded numbers you'll need to add up to the entered total.
This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
How to get random element in jquery?
(7 answers)
Closed 8 years ago.
I have 8 divs to select from, they all have id's assigned to them, named not numerically, but descriptively. I want to be able to add .myClass to a div chosen at random out of these eight.
To generate a random number, I would use this JavaScript snippet:
var random = Math.round(Math.random()*10);
My questions:
How can I limit the random number to only 1 out of 8 possible values?
How can I add .myClass to a randomly chosen one div out of eight with a non-numeric id?
var randomNumber= 1 + Math.floor(Math.random() * 8);
How can I limit the random number to only 1 out of 8 possible values?
Store the 8 possible values in an array, A
Get a number between 0 and 7 (inclusive) using Math.random() and assign to X
The random number you want is A[X]
For ex:
var A = [2, 5, 6, 7, 8, 9, 11, 15];
var X = Math.floor(Math.random()*8);
var theNumber = A[X];
How about:
var random = Math.floor(Math.random()*8);
$("div:eq(" + random + ")").addClass("yourClassHere");
Edit: Was answering the question before it was edited, when selecting a random div was also needed.
Fiddle (thanks to smerney): http://jsfiddle.net/5JPWu/2/
var array= [2, 5, 6, 7, 8, 9, 11, 15];
var X = Math.floor(Math.random()*8);
Then use the following
varray[X];