get remainder function doesn't work [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 9 years ago.
Improve this question
I am trying to write a script in shorter way but it doesn't work..
It is not really clear as i see.. so: here all of the codes..
$(document).ready(function(){
$('button').click(function(){
var benen = $('[name=benen]').val();
var zadelmaat = benen * 1.89;
var kadermaat = benen * 0.69;
var remainder = zadelmaat % 2;
if(remainder % 2 == 0)
{
return zadelmaat;
}else
{
zadelmaat = zadelmaat - remainder;
}
var remainder = kadermaat % 2;
if(remainder % 2 == 0)
{
return kadermaat;
}else
{
kadermaat = kadermaat - remainder;
}
$('.zadelmaat').append(zadelmaat);
$('.kadermaat').append(kadermaat);
return false;
});
});
That works for me! But what i am trying to change is to write a simple function to get rid of the remainder(in case)!
I hope it s more clear now...
Thnx!

This will do it:
function getRemainder(x) {
var remainder = x % 2;
return x - remainder;
}
You forgot to return the result in case the remainder was not 0. And you don't need to explicitly test for that, as x - 0 == x.

You pass a value to the function, not a pointer. To make it work you must add return x; to the else clause.

Change your function to this:
you only return a different value if it is not ==0
function getRemainder(x) {
var remainder = x % 2;
if(x % 2 != 0) {
x = x - remainder;
}
return x
}

Someone edited your code which seems to be an invalid edit as it really changed the question/problem.
Simplified code for function:
function getRemainder (x) {
return x % 2 === 0 ? x : x = x - x % 2;
};
I made some assumptions from the original code in a sample fiddle: http://jsfiddle.net/eqQzM/
alerts "28:6"

Related

Debugging small javascript with exponents [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
n = 1;
while (n < 1024) {
alert(Math.pow(n, 2));
n = n + 2;
}
How can I make this display the powers of 2 from 2^0 to 2^10 inclusive i.e. (1-1024) and end once it reaches 1024 in Javascript
I would do something like this:
for(let n = 0; n <= 10; n++) {
console.log(2 ** n)
}
This logs the answer of each loop iteration. As Pointy suggested in the comment, we have to make the n value iterate from 1 to 10, since that is the variable that generates the output, whereas the 1024 is simply the output that comes from the power values. You could also use Math.pow as follows:
for(let n = 0; n <= 10; n++) {
console.log(Math.pow(2, n))
}
Implementing Secan's suggestion, here is a function which will take any number as a parameter:
function foo(x, exp) {
for(let i = 0; i <= exp; i++) {
console.log(x ** i)
}
}
So to apply this to the original answer, we would call it like this:
foo(2, 10)
Sorry for the ambuiguity with my original question. I've figured it out-
n=0;
while (n <= 10) {
alert(Math.pow(2, n));
n = n + 1;
}
Setting n=0, n<=10 in while and changing n + 1 instead of 2 resolved my issue. Thanks for your responses!

JavaScript Function. Can someone help or explain why it logs 120? I see 20 based on my analysis [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 would like to sum up all the elements inside the array covertedValue. Why I am getting a Nan result? whats wrong with the recursive function I have written?
function findOutlier(integers){
var covertedValue = integers.map(x => x % 2);
var total = 0;
for (i=0 ; i<covertedValue.length ; i++){
total = total + covertedValue[0] + findOutlier(integers.splice(1));
}
console.log(total);
}
findOutlier([0, 1, 2]);
because this line create a function and a private alias to itself
var factorial =
// create a variable named factorial in global scope
// it contains the function fac
function fac(n) {
// fac is a function available only in the factorial var context
return n < 2 ? // if n < 2
1 : // return 1
n * fac(n - 1); // else return n * fac(n - 1)
};
console.log(factorial(5));
// calls factorial(5) which is 5*fac(4) which is 5*(4*fac(3)) ...
note that the fac function is not available outside of itself
var factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1); };
console.log(fac(5));
This way of coding where a function call itself is called recursive programming it can be hard to grasp at first but can be VERY powerful in some situations
recurcive programming can easily be used when the result of func(n) depend directly on the result of func(n+x)
the cond ? then : else structure is called ternary operator, it's a way to make if in one line, can become messy very quick
as you noted in a comment : recursion can be replaced by a loop (it can be hard to do it sometime)
here's a more beginner way to write it
function factorial(n) {
if(n < 2) {
return 1
} else {
let fac = 1;
for(var i = n; i > 0; i--) {
fac = fac * i
// here fac takes the value of the precedent iteration
}
// this whole loop is the same as i*fac(i-1) in the recurcive way
return fac
}
};
console.log(factorial(5));

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

Multiply two numbers without using multiplication [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 5 years ago.
Improve this question
I have a test, where I needed to code for multiplying two numbers without using multiplication,
the code is as follows,
function multiply(num,toNum){
var product = 0;
for(var i = 1; i <= toNum; i++){
product += num;
}
return product;
}
console.log(multiply(2,5));
The output is
rahul#rahul:~/myPractise/Algo$ node MultiplyWithoutLoop.js
10
rahul#rahul:~/myPractise/Algo$
Is the above code satisfactory or need is there a room for improvement.
Can a better logic be applied.
Hey,
I solved it using recursion,
this is the code,
function multiply01(num,toNum){
var product = num;
return (toNum >= 1) ? product + multiply01(product,--toNum) : 0;
}
Compact way:
function multiply(a, b) {
return a / (1 / b);
}
console.log(multiply(2, 5)); // 10
You could use addition for odd numbers and and bit shifting. Better known as Ancient Egyptian multiplication.
The value of b is summed, if a is odd. Then a is divided by 2 and the integer part is assigned. b is doubled.
Example:
a b sum
--- --- ---
5 4 4 add 4
2 8 4
1 16 20 add 16
0 32 20 <- result
function multiply(a, b) {
var sum = 0;
while (a) {
if (a & 1) {
sum += b;
}
a >>= 1;
b <<= 1;
}
return sum;
}
console.log(multiply(5, 4));
console.log(multiply(3, 7));
console.log(multiply(191, 7));
Please try this, it will also handle negative value
function multiply(x, y)
{
/* Add x one by one */
if(y > 0 )
{
return (x + multiply(x, y-1));
}
/* the case where y is negative */
else if(y < 0 )
{
return -multiply(x, -y);
}
return 0;
}

Return random based off percentage [duplicate]

This question already has answers here:
Generate A Weighted Random Number
(14 answers)
Closed 7 years ago.
I've been trying to figure out a way to accomplish this, but I'm running into a wall and I figured I would ask here.
Here is an example:
Let's say I have two objects, food and water, and they have different 'chance' values. One has a 20% chance, one a 40% chance. How would I be able to mathematically add to their total, given that percentage?
var food = {
name:'food',
total:0,
chance:0.2
};
var water = {
name:'water',
total:0,
chance:0.4
};
This is what I have so far, but this isn't really percentage based. Looking for ways to improve it.
function test() {
var x = parseFloat(Math.random().toFixed(1));
if(x == food.chance) {
food.total += 1;
}
if(x == water.chance) {
water.total += 1;
}
}
Close.
function test() {
var x = parseFloat(Math.random().toFixed(1));
if(x < food.chance) {
food.total += 1;
}
else if(x < food.chance + water.chance) {
water.total += 1;
}
}
Two notes:
In continuous probabilities, you almost never check some random output equals something; just that it's between somethings. Almost by definition, the former is 0.
This still leaves a 40% that neither happened, FYI.
you need something like this:
if ( (x >= 0.0) && (x < food.chance) ) food.total++;
else if ( (x >= food.chance) && (x < (food.chance+water.chance)) water.total++;

Categories

Resources