Increase values of elements in an array [closed] - javascript

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 3 years ago.
Improve this question
Say I have an array like so...
let myArr = [0,0,2,0,0];
I want to create sort of a ripple effect such that the result of the array is [0,1,2,1,0]

This would give you the result you expect:
let myArr = [0, 0, 2, 0, 0];
createRippleArray = (myArr) => {
if (myArr.length % 2 === 0) {
console.error("createRippleArray: Array length needs to be odd number>1");
return [];
}
let midIndex = ~~(myArr.length / 2);
let mid = myArr[midIndex];
return myArr.map((e, i) => {
let res;
if (i < midIndex) {
return ~~(mid / Math.abs(midIndex - i + 1));
} else if (i === midIndex) {
return mid;
} else if (i > midIndex) {
return ~~(mid / Math.abs(midIndex - i - 1));
}
});
}
console.log(createRippleArray(myArr));
Hope this helps!

Related

How to refactor this code with if statement [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
I have code with if statement (below). Is it possible to refactor this code? because in this method is ugly if statement :(
getOverallAmount(estimate) {
const {installments} = estimate;
const {vipValue} = estimate;
if (installments !== 1) {
const oneInstallment = vipValue / installments;
const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
return oneInstallment + allWithOneInst;
}
return vipValue;
}
Using a ternary operator instead of the if statement:
getOverallAmount(estimate) {
const {installments} = estimate;
const {vipValue} = estimate;
const oneInstallment = vipValue / installments;
const allWithOneInst = vipValue - (oneInstallment * installments) + 1;
return (installments !== 1) ? oneInstallment + allWithOneInst : vipValue;
}

Want to Sort with Loop In JS [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 years ago.
Improve this question
let fruits = [mango,banana,avocado,apple,orange,lychee];
let prices = [50,90,65,300,600,900]; // not constant value;
//Solution with If else
if(prices > 0 && prices <= 50) console.log("Mango#0-50")
if(prices > 51 && prices <= 65)console.log("Mango#0-50<br>Banana#51-65")
//So on
Is there any way to short it with loop?
This is how the result should look like
Mango#0-50
Banana#51-65
avocado#65-90
apple#91-300
orange#301-600
lychee#601-900
rest#>901
Note: I do not want to use If else;
let i = 1
fruits.map(fruit => `${fruit.name}#${i}-${i+=100}`);
You could map the fruits with their price range and slice the array by the wanted length and return a joined string.
function getValues(price) {
return temp
.slice(0, (prices.findIndex(p => price <= p) + 1) || prices.length + 1)
.join('<br>');
}
const
fruits = ['mango', 'banana', 'avocado', 'apple', 'orange', 'lychee'],
prices = [50, 90, 65, 300, 600, 900].sort((a, b) => a - b),
temp = [...fruits.map((f, i, { length }) => `${f}#${prices[i - 1] + 1 || 0}-${prices[i]}`), `rest#>${prices[prices.length - 1] + 1}`];
console.log(getValues(100));
console.log(getValues(300));
console.log(getValues(301));
console.log(getValues(1000));

sort array by specific pattern [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 years ago.
Improve this question
I want to get the array from:
const foo = [
FOO_BAR_A_READ_SELF,
FOO_BAR_A_WRITE_SELF,
FOO_BAR_A,
FOO_BAR_A_READ_ALL,
FOO_BAR_A_WRITE_ALL,
FOO_BAR_B_READ_SELF,
FOO_BAR_B_WRITE_SELF,
FOO_BAR_B,
FOO_BAR_B_READ_ALL,
FOO_BAR_B_WRITE_ALL
]
to
const foo = [
FOO_BAR_A,
FOO_BAR_A_READ_SELF,
FOO_BAR_A_WRITE_SELF,
FOO_BAR_A_READ_ALL,
FOO_BAR_A_WRITE_ALL,
FOO_BAR_B,
FOO_BAR_B_READ_SELF,
FOO_BAR_B_WRITE_SELF,
FOO_BAR_B_READ_ALL,
FOO_BAR_B_WRITE_ALL
]
i tried to go with the length by splitting with the "_", but I never worked with the sort function that specific.
I only used desc and asc ( return 1 > -1 ) || ( return -1 > 1 )
Can someone can explain me how I can get the wanted result?
Not sure it was my best...
var arr = [
'FOO_BAR_A_WRITE_SELF',
'FOO_BAR_A',
'FOO_BAR_A_READ_SELF',
'FOO_BAR_A_READ_ALL',
'FOO_BAR_A_WRITE_ALL',
'FOO_BAR_B_READ_SELF',
'FOO_BAR_B_WRITE_SELF',
'FOO_BAR_B',
'FOO_BAR_B_READ_ALL',
'FOO_BAR_B_WRITE_ALL'
];
const onRWorder=s=>{
let x = s.replace('_WRITE_','_')
if (x != s) x += '_WRITE'
else {
x = s.replace('_READ_','_')
if (x!= s) x += '_READ'
}
return x
}
arr.sort(function(a, b){
let a1 = onRWorder(a)
let b1 = onRWorder(b)
if(a1 < b1) { return -1 }
if(a1 > b1) { return 1 }
return 0
})
for (let z of arr) console.log(z)
.as-console-wrapper { max-height: 100% !important }
did you try this ?
var arr = [
'FOO_BAR_A_WRITE_SELF',
'FOO_BAR_A',
'FOO_BAR_A_READ_SELF',
'FOO_BAR_A_READ_ALL',
'FOO_BAR_A_WRITE_ALL',
'FOO_BAR_B_READ_SELF',
'FOO_BAR_B_WRITE_SELF',
'FOO_BAR_B',
'FOO_BAR_B_READ_ALL',
'FOO_BAR_B_WRITE_ALL'
];
var sorted = arr.sort();
console.log(sorted);

how to find the sum of all integers present in a string which is divisible by 3? [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
Find the sum of all the numbers in a string which is divisible by 3 and also find the last such number (Use JavaScript). Example “The best 6 of 8 will get 9 points”, sum = 15, last=9.
Sure - use split, reduce and filter with % (modulo) for divisibility:
const str = "The best 6 of 8 will get 9 points";
const strArr = str.split("");
const threesArr = strArr.filter(e => parseInt(e) % 3 == 0);
const sumOfThrees = threesArr.reduce((acc, curr) => acc + parseInt(curr), 0);
const allNumbers = strArr.filter(e => parseInt(e));
const lastNumber = allNumbers[allNumbers.length - 1];
console.log("Sum: " + sumOfThrees);
console.log("Last: " + lastNumber);

Parse poisson function in javascript to php [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 have the next code in javascript:
var exponential = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746;
var numerator, denominator;
function fact(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
function poisson(k, landa) {
exponentialPower = Math.pow(exponential, -landa); // negative power k
landaPowerK = Math.pow(landa, k); // Landa elevated k
numerator = exponentialPower * landaPowerK;
denominator = fact(k); // factorial of k.
return (numerator / denominator);
}
I need parse to php but i don't know how...
Can somebody help me?
you should put below variables within poisson function also dont think you need to initialize $numerator and $dominator to 0;
$exponential = 2;
$numerator = 0;
$dominator = 0;
function fact($x) {
if($x==0) {
return 1;
}
return $x * fact($x-1);
}
function poisson($k, $landa)
{
$exponential = 2;
$exponentialPower = pow($exponential, -$landa);
$landaPowerK = pow($landa,$k);
$numerator = $exponentialPower * $landaPowerK;
$dominator = fact($k);
echo ($numerator / $dominator);
}
poisson(1,2);

Categories

Resources