JavaScript Math Code Explanation [closed] - javascript

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 8 years ago.
Improve this question
if(MathRScore > 29.9 && MathRScore < 34.9 ) MathScore = 10*Math.round((MathRScore*45/5+(535*35-580*30)/5)/10);
I've inherited this tiny line of exception code for a scoring algorithm I'm maintaining. I'm not sure what it does exactly. Can someone walk me through it?

If your MathRScore is in the interval (29.9, 34.9) then set the MathScore to (in simplified form)
MathScore = 10*Math.round((MathRScore*9+265)/10)
By dividing by 10, then using Math.round and then multiplying by 10 you will round to the nearest ten.
For example
10*Math.round(1111/10) = 10*Math.round(111.1) = 10*111 = 1110

if the value of MathRScore is between 30 and 34.8 , it will make the variable MathScore hold the value of 10 times the rounded value of all that stuff in the parenthesis.

If MathRScore is between 29.9 and 34.9, exclusive, then set MathScore to the value:
(MathRScore * 9) + 265
rounded to the nearest 10.
Beyond that, what this means can only be revealed by understanding how these scores work, and we can't tell you that.

Related

adding a percentage to a big integer [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 11 months ago.
Improve this question
I'm making a cookie clicker game where i recently converted the counter from a number primitive to Big-int and i have faced a problem where i need to add a percentage onto a Big-int. Normally if i would like to add 50% of 1000 to 1000 it would be as easy as multiplying 1000 by 1.5 but you cannot do that with Int's. I am open to ideas on how to solve this issue, any bit helps. What I was originally thinking is that I divide it by 100 and then multiply it by 150 which would give the same results, but the problem is that if the original was below 100 then the fact that it is an int would mess this process up.
You can do that with bigInts too:
const bigIntNum = BigInt(123456);
const multipledByTwo = bigIntNum * 2n;
console.log(multipledByTwo);
But do make sure the number that you are calculating by multiplying should be in the range of bigInt.
To add some percent to the bigInt number, you should write the following code:
const bigIntNum = BigInt(123456);
//adding 50%
const addedFiftyPercent = bigIntNum + (bigIntNum * 50n) / 100n;
//adding x%
const addedXPercent = bigIntNum + (bigIntNum * xn) / 100n;

Javascript parseFloat() change number [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 4 years ago.
Improve this question
I have a strange problem with JavaScript .
I have text value in a textbox '25000000' when i am
using parsefloat(txt.value), the returned value is 25000 !??
Why is 25000000 changed to 25000 ?
I think there is an alphabet somewhere in your numbers. Check the example below.There is an error in your code.
parseFloat(" 250000000 ") = 250000000
parseFloat("2018#geeksforgeeks") = 2018
parseFloat("geeksforgeeks#2018") = NaN
Thanks for all Responses.
The problem was for ',' in price.
Of course we replace ',' with '' ,but replace method
only change first one like "25,000,000,000"=>"25000,000,000".
I used str.split(',').join('') and problem solve and parsefloat returned correctly.
regards
ali

Using a while loop to print random numbers until a certain number is reached [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 7 years ago.
Improve this question
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.

Method position error [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 8 years ago.
Improve this question
I want to calculate 17% from 835 and after reduce from n the resulted number.
But the resulted number is 693.5 and I want to show downwards to the nearest integer 693.
The Math.floor should do that but I don't understand why the resulted number shown is 694.
Thank you.
function compute_active_users(n, b) {
// Write your code here
var s =b/100*n;
var b =Math.floor(s);
console.log(n-b);
}
compute_active_users(835,17);
Okey you should do
function compute_active_users(n, b) {
// Write your code here
var s = b /100 *n;
var b = Math.floor(n - s);
console.log(b);
}

Figure out if number is a minus or a positive number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Say I have a range which consists of < -10 and I split this up using a regex call which leaves me with < -10.
I then have a function which gets me the number from the split and I call it like range1.getMin(), this would return -10 but when I use range1.getMin().indexOf('-') it doesn't work.
Try comparing to zero:
var isNegative = range1.getMin() < 0;
function isMin(value) {
if(value<0) {
return true;
}
return false;
}
You could add a check like eval() for the value to make sure you're dealing with an integer.
Also, if you need to make sure you have a positive number (or negative number for that matter) before you use the number in your process, you can Math.abs() your number to make sure it's always a positive number.
var val = parseInt("-10", 10)
can be used to parse integer and to test for positive number
val >= 0

Categories