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

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

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

Java/JS/TS Given row and column get bit value in truthtable [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 3 months ago.
Improve this question
Is their a short formula for calculating the bit value for a given row and column?
Example: getTTBit(row = 3, col = 2) = 1
4 2 1
0 0 0
0 0 1
0 1 0
0 1 1 <-- row 3, col 2 -> 1
1 0 0
1 0 1
1 1 0
1 1 1
It should be as quick as possible. I dont want to convert the row number into a bitarray and get the col'th element because I work with large numbers (rows). My bad current solution looks like this (TS):
export function getTTBit(numCols: number, row: number, col: number): bit {
return getNumberAsBitArray(row, numCols)[col];
}
export function getNumberAsBitArray(n: number, length: number): bit[] {
return [...Array(length)].map((_, i) => n >> i & 1).reverse() as bit[];
}
basically, you're asking how to check if the Nth bit is set in a number?
This is the same in all three languages:
isSet = (row & (1 << col)) != 0
where col is counted from the right (lsb=0).

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]

javascript next number on multiples of three [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a number for example 4, i want to get next number on multiples of 3
Multiples of three: [3,6,9,12,15,18,21,24,27,30,...]
the result must be 6
i'm looking for a javascript function
something like this:
function (myNum) { //myNum = 4;
var multiples = [3,6,9,12,15,18,21,24,27,30];
var result;
// do something!!
return result; // returns 6
}
thanks
I suggest another solution:
function getNext(num, dep){
return (((num % dep) ? dep:0) - num % dep) + num;
}
document.write(getNext(4, 3));//6
//document.write(getNext(200, 7));//203
Updated: You can use this method for finding next number on multiples of any number
There are a lot of ways you can achieve this. Here is an easy one. Increment the number until you get a multiple of three.
function multipleOfThree(num){
while(num % 3 != 0)
num++;
return num;
}
You must try before asking a question. If you stuck at somewhere then it is good to ask questions with problem. By the way here what you can try:
function multiple(number) {
return number % 3 === 0 ? ((number/3) * 3) : parseInt((number/3) + 1) * 3;
}

Categories