Check if array has value from and to a variable [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 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);

Related

Why is my JavaScript code not accepted as the right answer? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to do this Javascript exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
I am wondering why the solution below is not an accepted answer:
let count = 0;
function cc(card) {
// Only change code below this line
const low = [2, 3, 4, 5, 6];
const high = [10, 'J', 'Q', 'K', 'A'];
if (low.includes(card)) {
count += 1;
}
else if (high.includes(card)) {
count -= 1;
}
let decision;
if (count > 0) {decision = "Bet"}
else {decision = "Hold"}
return count + decision;
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
When I am comparing it to accepted answers I don't see what they are doing differently. One thing that is not clear to me in the assignment is that should return be called every time or only after the last function call (cc('A');).
Add a space between count and decision
return count + " " + decision;
You are giving an answer in the wrong format. Just missing the space between count and decision.
Incorrect:return count + decision;
Correct:return count +" "+ decision;

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

Find Min and Max with javascript [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 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])

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