nth roots in JavaScript [duplicate] - javascript

This question already has answers here:
JavaScript: Calculate the nth root of a number
(9 answers)
Closed 1 year ago.
I was working on a simple little math project that is intended to help with exponential functions and stuff. I want to calculate a from f(x), x and c. So the Formula for that is pretty simple, it's just the x root of f(x) divided by c. But I can't find a way to take a specific nth root. I read something about Math.pow() and played around with it, but can't really get it working.
Those are my current lines of code:
if (fx != null && c != null && x != null) {
var a = <the part i need>
aoutp.innerHTML = "a = " + a;
}
Hope you understand what I need :)

Math.pow(x, 1/y) is y-root of x:
const pow_4 = Math.pow(2, 4) // 2^4
console.log(pow_4) // 16
const sqrt_4 = Math.pow(pow_4, 1/4) // sqrt root lvl 4 of 16
console.log(sqrt_4) // 2

Related

A task in JavaScript

I need to create a sequence of numbers using while or for that consists of the sum of the symbols of the number.
For example, I have a sequence from 1 to 10. In console (if I've already written a code) will go just 1, 2,3,4,5,6,7,8,9,1. If I take it from 30 to 40 in the console would be 3,4,5,6,7,8,9,10,11,12,13.
I need to create a code that displays a sum that goes from 1 to 100. I don't know how to do it but in console I need to see:
1
2
3
4
5
5
6
7
8
9
1
2
3
4
etc.
I've got some code but I got only NaN. I don't know why. Could you explain this to me?
for (let i = '1'; i <= 99; i++) {
let a = Number(i[0]);
let b = Number(i[1])
let b1 = Boolean(b)
if (b1 == false) {
console.log ('b false', a)
}
else {
console.log ('b true', a + b)
}
}
I hope you get what I was speaking about.
Although I like the accepted answer however from question I gather you were asking something else, that is;
30 become 3+0=3
31 become 3+1=4
37 becomes 3+7=10
Why are we checking for boolean is beyond the scope of the question
Here is simple snnipet does exactly what you ask for
for (let i = 30; i <= 40; i++) {
let x=i.toString();
console.log( 'numbers from ' +i + ' are added together to become '+ (Number(x[0])+Number((x[1])||0)))
}
what er are doing is exactly what Maskin stated begin with for loop then in each increment convert it to string so we can split it, this takes care of NAN issue.
you don't need to call to string just do it once as in let x then simply call the split as x[0] and so on.
within second number we have created a self computation (x[1])||0) that is if there is second value if not then zero. following would work like charm
for (let i = 1; i <= 10; i++) {
let x=i.toString();
console.log( 'numbers from ' +i + ' are added together to become '+ (Number(x[0])+Number((x[1])||0)))
}
Did you observe what happens to ten
here is my real question and solution what if you Don't know the length of the digits in number or for what ever reason you are to go about staring from 100 on wards. We need some form of AI into the code
for (let i = 110; i <= 120; i++) {
let x= Array.from(String(i), Number);
console.log(
x.reduce(function(a, b){ return a + b;})
);
};
You simply make an array with Array.from function then use simple Array.reduce function to run custom functions that adds up all the values as sum, finally run that in console.
Nice, simple and AI
You got NaN because of "i[0]". You need to add toString() call.
for (let i = '1'; i <= 99; i++) {
let a = Number(i.toString()[0]);
let b = Number(i.toString()[1])
let b1 = Boolean(b)
if (b1 == false) {
console.log('b false', a)
} else {
console.log('b true', a + b)
}
}
So the way a for loop works is that you declare a variable to loop, then state the loop condition and then you ask what happens at the end of the loop, normally you increment (which means take the variable and add one to it).
When you say let i = '1', what you're actually doing, is creating a new string, which when you ask for i[0], it gives you the first character in the string.
You should look up the modulo operator. You want to add the number of units, which you can get by dividing by 10 and then casting to an int, to the number in the tens, which you get with the modulo.
As an aside, when you ask a question on StackOverflow, you should ask in a way that means people who have similar questions to you can find their answers.

Round the number with JavaScript [duplicate]

This question already has answers here:
How do I round a number in JavaScript?
(8 answers)
Round number up to the nearest multiple of 3
(13 answers)
Closed 4 years ago.
I want to round the numbers like this:
Value Expected
0,523% 1%
2,235% 2,5%
-0,081% -0,5%
-1,081% -1,5%
How can I do this with JavaScript?
Solution:
static round (num) {
const abs = Math.abs(num);
const sign = num < 0 ? -1 : 1;
return sign * (abs % 1 > 0.5 ? Math.ceil(abs) : Math.floor(abs) + 0.5) }
}
I used Excel for it =round(Value/Granularity)*Granularity but I tried it in JavaScript not working.
static calc(num){
const granularitiy = 0.00005;
let calc = Math.round((num / granularitiy)) * granularitiy;
return calc;
}
My granularity value is 0.00005;
Since on only want to round to .5 or round numbers, you can't simply use Math.round(number).
The easiest way to do this would be to have something like:
Math.round(number*2)/2
This way, you'll alway have a number .5 or round.
If you want to round another way (ceil or floor), you can simply replace the Math.round by whatever you want.

How do I use the '^' in this function in JavaScript? [duplicate]

This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 5 years ago.
Write a function called "computeCompoundInterest".
Given a principal, an interest rate, a compounding frequency, and a
time (in years), "computeCompoundInterest" returns the amount of
compound interest generated.
var output = computeCompoundInterest(1500, .043, 4, 6);
console.log(output); // --> 438.8368221341061
Reference:
https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest
This shows the formula used to calculate the total compound interest generated.
Problem is i am trying to use this formula and can't get it right
function computeCompoundInterest(p, i, compoundingFrequency, timeInYears) {
p = p * (1 + (i/4)))^(compoundingFrequency*timeInYears)
}
I tried to step through each calculation and it looks like once I get to:
p = 1500 * (1 + 0.043/4)^ 4 x 6 //compoundingFrequency = 4 and timeInYears = 6
I am doing something wrong. This website seems to get a decimal number when you (1 + (i/4)))^(compoundingFrequency*timeInYears)
^ operator is XOR operator.
For exponentation you should use function Math.pow (like Math.pow(2,3) === 8) or ** operator (like 2**3 === 8)

Adding large decimals number in JavaScript [duplicate]

This question already has an answer here:
Is there a limit in Javascript to decimal places on big floats?
(1 answer)
Closed 6 years ago.
I am trying to add this large decimal number together to get 9999999999.9999999999 but instead I am getting 10000000000.
Here is the code:
function add(x, y) {
var filterFloat = function (value) {
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
}
if (typeof x == 'string') {
filterFloat(x);
console.log(x);
};
if (typeof y == 'string') {
filterFloat(y);
console.log(y);
};
var z = filterFloat(x) + filterFloat(y);
//var short = z.toFixed(4);
//console.log(short);
//return short.toString();
//return z.toString();
return z;
var zz = z.toString();
return zz;
//console.log(typeof zz);
}
Here is the argument being passed:
add("1234567890.0987654321", "8765432109.9012345678");
See the fiddle below:
https://jsfiddle.net/emporio/gythd5dr/
This is most probably floating-point precision at work. In general floating-point numbers as computed by modern CPUs are only approximately right and have lots of edge cases like the one you are experiencing. That's due to a finite length of the registers in your CPU.
If you need arbitrary precision, you'll need to fallback to a (much slower) library that handles it (or do it yourself manually of course).
Example: https://github.com/MikeMcl/bignumber.js
Have you tried this so far?
https://shamim8888.wordpress.com/languages/bigdecimal-in-javascript/
I think that this might work for you. I used it before.

Best method to take the random number of the range in JavaScript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random numbers in Javascript in a specific range?
Best method use Math object for example to take the random number of:
[a, b] (double - integer) (2 cases)
(a, b) (double - integer)
[a, b) (double - integer)
(a, b] (double - integer)
Anybody can help me? Thanks!
I guess this is what you're looking for:
var rand = function(a,b){
return a+Math.round((b-a)*Math.random());
}
var r = rand(5,10);
console.log(r);
Math.extendedRandom = function(a,b,excludeA,excludeB, round)
{
var start = a;
var end = b;
if(excludeA)
start++;
if(excludeB)
end--;
var res = (end-start) * Math.random();
return start + (round ? Math.floor(res) : res);
};
The simplest case is the third one, which is already the implementation of Math.random in JavaScript.
The first case is answered by Trevor in this question. Make sure to read the other answers and comments, though, to get a clear understanding of the implications.
Likewise, the fourth case is answered by nickf in that same question.
The second case could be handled by a variation of Trevor's answer:
function zero_exclusive_random(){
var r = Math.random();
return r == 0 ? zero_exclusive_random() : r;
}

Categories

Resources