Find Min and Max with javascript [closed] - javascript

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])

Related

Check if array has value from and to a variable [closed]

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);

Is there an any way to do get range [closed]

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

I need to add a number from right to left in the decimal value using javascript [closed]

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 4 years ago.
Improve this question
I am trying to display decimal value in calculator using javascript.
if i click any buttons in the calculator, the button value added to decimal value from the right to left and it should be display in the screen.
For Eg: The default value is 0.00 if i click 2, it should be 0.02 and if i click 3, it should be 0.23 and if i click 4, it should be 2.34 and if i click 5, it should be 23.45 and so on.
You could try:
let number = 0;
// Returns the new value in case you need it to display in console
// and updates the number variable with the new value.
function addNumberRightToLeft(value) {
number = ((number * 10) + (value / 100)).toFixed(2);
return number;
}
console.log(addNumberRightToLeft(5)); // Shows '0.05' in console and updates number variable, so it is now '0.05'.
// or
addNumberRightToLeft(5); // Does not print to console but updates number variable, so it is now '0.55'.
or even use ES6 arrow functions and without side effects (as suggested by Nika):
const addNumberRightToLeft = (p, v) => ((p * 10) + (v / 100)).toFixed(2);
where p is the last value returned and v is what you want to add. In action:
let number = 0;
number = addNumberRightToLeft(number, 5); // 0.05
number = addNumberRightToLeft(number, 5); // 0.55
Like below: resultNum should be first initialize as 0.00;
function showCalc(inputNum)
{
var a = resultNum * 10.00;
var b = inputNum / 100;
resultNum = a + b;
return resultNum;
}

Javascript regex to parse human readable dates [closed]

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 7 years ago.
Improve this question
I have a dates in String in Javascript that could look like:
1h
1h2m
1d3m4s
2d2h2m2s2ms
1ms
3s5ms
The indicators will not change, they are d, h, m, s, ms
What would be a good regex to parse the numbers out:
for 3s5ms, it should be:
parsed = [0,0,0,3,5]
for 1d4m, it should be:
parsed = [1,0,4,0,0]
How about this:
var getNumbers = function (string) {
var numbersArray = string.match(/(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?(?:(\d+)ms)?/);
numbersArray.shift();
return numbersArray.map(function (val) {
return parseInt(val) || 0;
})
};
getNumbers("3s5ms") // [0, 0, 0, 3, 5]
getNumbers("2d2h2m2s2ms") //[2, 2, 2, 2, 2]

Is there a name for a formula to calculate ascending numbers to a quadratic-like sequence? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
For e.g. any range of number 0 - n
[ 0, 1, 2, 3, 4, 5, 6 ]
to:
[ 0, 2, 4, 6, 4, 2, 0 ]
IS there a formula to calculate the first into the second? Quadratic?
Is there a name for this kind of formula or calculation?
EDIT: This should be in Javascript
I don't know what you mean by "quadratic-like" but the following javascript program prints something which looks like your sequence:
n = 6;
for(i=0;i<=n;i++){
print(i, n-Math.abs(2*i-n))
}
Output:
0 0
1 2
2 4
3 6
4 4
5 2
6 0

Categories