js math function for y√(x)? - javascript

3√(9) = 2.0800838231, or Y√x
is there any Math function in vanilla js for calculation above the formula ?

Can you try this:
console.log(Math.pow(9, 1/3)); // replace "3" with the root
// "9" with your number

This is what i think will work:
document.write(Math.pow(9, 1 / 3));
pow raises the second argument to the power of first argument.
Now from basic math we know 3√(9) = (9)^(1/3)

The Math.cbrt() function returns the cube root of a number.
Math.cbrt(x) returns y where x is a number and y * y * y = x.
For example, cube root of 27 = 3 , cube root of 8 = 2.
Math.cbrt(27) = 3.
Here x = 27 and y = 3. So, y * y * y = x. So, 3 * 3 * 3 = 27.
console.log(Math.cbrt(8),2);
console.log(Math.cbrt(27),3);
console.log(Math.cbrt(9),2.080083823051904);

Related

rounding up to the multiplier

I am writing a javascript code which gives output of a X which always divisible by Y
For example if I have 7 which is not divisible by 4 but I want the code to add number up to 8
This is the code I did, it works for 7, but it should work for value 8
var x = 7
var y = 4
x = x + (y - x%y)
console.log(x)
I get the output 8 which is correct, but
var x = 8
var y = 4
x = x + (y - x%y)
console.log(x)
I am getting 12 which is not correct, I want the same 8
My question is, without putting an if condition how I will make this formula work?
If I understood correctly, you're probably looking for something like this:
Math.ceil(x / y) * y;
function toNearestMultiple(x, y) {
return Math.ceil(x / y) * y;
}
console.log(toNearestMultiple(7, 4));
console.log(toNearestMultiple(8, 4));
console.log(toNearestMultiple(9, 4));
This should also do it:
var x = 8
var y = 4
x = x+y-(x-1)%y-1
console.log(x)

How reverse the order of an array [duplicate]

This question already has answers here:
What is the most efficient way to reverse an array in Javascript?
(18 answers)
Closed 11 months ago.
I currently use this bit of javascript inside Adobe After Effects to print a customizable list of numbers
x = 0 //starting point (the first number in the list);
y= 20 //increments (the size of the steps);
z= 5 //the number of steps;
Array.from(new Array(z)).map((_, i) => i * y + x).join("\n")
which (in this case) would output
0
20
40
60
80
It would be really cool if I could also generate the list in reverse
80
60
40
20
0
but because I'm not a programmer I have no idea how to do this. Could someone help me with this?
Instead of starting from the first number (x) and adding y for each iteration, you can start from the last number and subtract y for every iteration.
The last number would be the number of times y is added added to x. That gives the formulat (z - 1) * y) + x
x = 0 //starting point (the first number in the list);
y= 20 //increments (the size of the steps);
z= 5 //the number of steps;
const result = Array.from(new Array(z))
.map((_, i) => (((z - 1) * y) + x) - (i * y)).join("\n");
console.log(result);
I think there is no need to optimisation here, as the op has no programming experience, a simple solution is well enough, which would be the Array.reverse().
See the the MDN docs for correct usage.
const x = 0 //starting point (the first number in the list);
const y= 20 //increments (the size of the steps);
const z= 5 //the number of steps;
const array = Array.from({length: z}, ((_, i) => i * y + x)).reverse().join("\n");
console.log(array);
Another possible solution: simply populate your array from the back. Then no need to reverse.
const x = 0 //starting point (the first number in the list);
const y= 20 //increments (the size of the steps);
const z= 5 //the number of steps;
const array = Array.from({length: z}, ((_, i) => (z * y - y) - i * y)).join("\n");
console.log(array);

How do I solve this ? Javascript

Here is the problem:
Declare a variable called x and assign it a value that equals the remainder of 20 divided by 3. Next, divide and assign 1 to that variable and display the result in an alert box. The result should be 2.
Here is my code:
var x = 20 / 3;
alert(x / 1);
I am brand new to Javascript. I am coming up with 6.66. The answer should be 2. What am I doing wrong?
You need this:
the remainder of 20 divided by 3
You're dividing. The remainder (or modulo) operator in JavaScript is the percentage symbol (%).
So your code should look like this instead:
var x = 20 % 3;
alert(x / 1);
Further reading:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
https://www.w3schools.com/js/js_arithmetic.asp
Well there is % in JS for remainder. / is the division sign.
var x = 20 % 3; console.log(x / 1);

How to truncate extra zeros from floating point number

Say:
var x = 6.450000000000003;
var y = 5.234500000000002;
These are the results of floating point division, so the 3 and the 2 need to be removed. How can I trim x to 6.45 and y to 5.2345 given that they have different levels of precision?
You could use Number#toFixed and convert the string back to number.
var x = 6.450000000000003,
y = 5.234500000000002;
x = +x.toFixed(5);
y = +y.toFixed(5);
console.log(x);
console.log(y);
You could use Math.round but you must choose a precision.
(Otherwise, you are losing percision and you don't want that!)
var x = 6.450000000000003;
var y = 5.234500000000002;
console.log(Math.round(x * 1000000) / 1000000);
console.log(Math.round(y * 1000000) / 1000000);
Try this function. If, as you say, you're simply looking to remove the end digit and remove trailing zeros, the following code could help.
function stripZeroes(x){
// remove the last digit, that you know isn't relevant to what
// you are working on
x = x.toString().substring(0,x.toString().length-1);
// parse the (now) String back to a float. This has the added
// effect of removing trailing zeroes.
return parseFloat(x);}
// set up vars for testing the above function
var x = 6.450000000000003;
var y = 5.234500000000002;
// test function and show output
console.log(stripZeroes(x));
console.log(stripZeroes(y));

Calculate variable based on remainder

How do I use JavaScript to calculate the x value in this formula?
(x * y) % z = 1
y and z are known integers.
For example, y = 7, z = 20. 3 multiplied by 7 results into 21, that divided by 20 results into remainder of 1. Solution x = 3.
(3 * 7) % 20 = 1
This is a math question, not a JavaScript question. Use the Extended Euclidean Algorithm. For example, to find the inverse of 7 modulo 20, start with these two equations:
20 = 0•7 + 1•20.
7 = 1•7 + 0•20.
Next, divide the two numbers on the left (20/7) and take the integer part (2). The subtract that times the bottom equation from the one above it:
20 = 0•7 + 1•20.
7 = 1•7 + 0•20.
6 = -2•7 + 1•20.
Repeat: The integer part of 7/6 is 1. Subtract one times the bottom equation from the one above it. The new equation is:
1 = 3•7 - 1•20.
Now you can see that 3 times 7 is 1 modulo 20. (Simultaneously, -1 times 20 is 1 modulo 7.)
Well, there's more than one number that is valid for x. x could also be 23 in this case (23 * 7 = 161, and 161 % 20 = 1). So you need to express the problem a bit differently as a starting point, such as "what is the lowest x that can solve the equation?"
If you're solving that then it is suddenly a different problem. You then only need to solve for two possibilities: (x * y) - z = 1, and (x * y) = 1. From there you can do a little algebra to solve for x.

Categories

Resources