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;
}
Related
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!
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);
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);
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 4 years ago.
Improve this question
I have the following working just fine. Anything under 100 is taken as itself while more than that is capped at 100. While it works fine, I would like to write it as a maths formula for neatness. Any ideas?
var ceiling = 100;
var incrementSize = 10;
var width = ""
if (score*incrementSize<ceiling)
{
width = "" + incrementSize * score + "%";
}
else
{
width = "" + ceiling + "%";
}
divj.style.width = width;
You could use a ternary operation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)
Basic syntax is:
("condition to be evaluated")? "return if true" : "return if false"
divj.style.width = (score*incrementSize<ceiling)? incrementSize * score + "%" : ceiling + "%";
This'll reduce things down to just one line.
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 8 years ago.
Improve this question
im trying to add and remove to 2 different variables in a code for a incremental game.
here is the code:
$("#autoPayCheckBuy").click(function () {
if (totalClicks >= 50) {
totalClicks -= 50;
totalMoneys += 50;
$("#total_moneys").text(totalMoneys);
$("#total_clicks").text(totalClicks);
}
});
and here is the code for the money/clicks:
var totalClicks = 0;
var totalMoneys = 100;
sorry that i cant give a exact problem, i just cant find a problem in the code :(
I can't find a problem in the code either. What exactly is your problem? Maybe you are missing the d.ready ?
var totalClicks = 0;
var totalMoneys = 100;
$( document ).ready(function() {
$("#autoPayCheckBuy").click(function () {
if (totalClicks < 50) return false;
totalClicks -= 50;
totalMoneys += 50;
$("#total_moneys").text(totalMoneys);
$("#total_clicks").text(totalClicks);
});
});