Math.pow() floating point number as exponent [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to pass a floating-point number as the second argument (exponent) to Math.pow(). I always get NaN when the passed number is not a whole number, like 0, 2, 7, you name it. Is there a working way in javascript to make this work?
(function () {
var notice = document.getElementById('notice');
var value = 0.0;
var interval = 0.02;
var timeInterval = 10;
function interpolation(x) {
var y = Math.pow(Math.e, x); // <<< HERE >>>
console.log(x, y);
return y;
}
function animation() {
var callAgain = true;
if (value >= 1) {
value = 1.0;
callAgain = false;
}
notice.style['opacity'] = interpolation(value);
notice.style['marginTop'] = (value * 20 + 20) + 'px';
value += interval;
if (callAgain) {
setTimeout(animation, timeInterval);
}
}
animation();
})();
PS: Please don't comment, that an opacity greater than 1 doesn't make any sense. I know that e^x; x > 0 will yield values greater than 1. I'll insert a proper function when I got this working.

The constant is Math.E, not Math.e

Related

Check numbers in a range that contain or are divisible by 7 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I don't know what is the problem with this code I just need to know
how many number from Start to end that divide by 7 or has 7.
const divideOrHasSeven = (start, end) => {
var x = 0;
for (i = start; i <= end; i++) {
if (i % 7 || i.toString().indexOf("7")) { // if it is divide by 7 or has 7
x += 1;
}
}
return x;
};
The problem is i.toString().indexOf('7') will always return a truthy value EXCEPT (ironically) when 7 is actually in the number (inthe first position - index zero)
Change your conditional to if (i%7===0 || i.toString().indexOf('7')>-1)
You had some problems with your code.
If you compare with this, I think you will understand where.
This i.toString().indexOf('7') will return -1 if 7 is not found in string, which will be evaluated as true.
You also had some syntax errors.
const divideOrHasSeven = (start, end) => {
var x = 0;
for (i = start; i <= end; i++) {
if (i%7 === 0 || i.toString().indexOf('7') >= 0) {
x += 1;
}
}
return x
}
console.log(divideOrHasSeven(1, 200));

Converting a 'for loop' from Javascript to Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to convert the following javascript code to python.
let speed = 50;
const lightCount = 1;
const lights = [[200, 10];
const isRed = (speed, dist, dur) =>
(18 * dist) % (10 * speed * dur) >= (5 * speed * dur);
for(let i = 0; i < lightCount; i++) {
if (isRed(speed, lights[i][0], lights[i][1])) {
speed--; // Reduce speed
i = -1; // Start again
}
}
console.log(speed);
I tried the following in python, but it is not working:
speed = 50
light_count = 1
lights = [[200, 10]
def is_red(speed, dist, dur):
if (18 * dist) % (10 * speed * dur) >= (5 * speed * dur):
return True
return False
for i in range(light_count):
if is_red(speed, lights[i][0], lights[i][1]):
speed -= 1
i = -1
print(speed)
The answer is supposed to be 36 but it is showing 49.
It always loops only once.
Please suggest a solution.
Thanks in advance
Editing i will not step the loop back. ints are immutable, so you are just editing the state of a temporary variable. You can try using a while loop, and just incrementing back i.
Example:
speed = 50
light_count = 1
lights = [[200, 10]
def is_red(speed, dist, dur):
if (18 * dist) % (10 * speed * dur) >= (5 * speed * dur):
return True
return False
i = 0
while i < light_count:
if is_red(speed, lights[i][0], lights[i][1]):
speed -= 1
# i -= 1
#i += 1
else:
i += 1
del i

How could i get numbers instead of Nan? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been doing some challenge to create Fibonacci numbers generator,and got stack. It seemed like i have a solution, but those NaN's is the last problem i cant't deal with.
function fibonacciGenerator (n) {
var output = [];
if( n === 2 ){
output.push(0,1);
}
else if( n === 1 ){
output.push(0);
}
else{
output = [0,1];
while( n > output.length){
output.push((output[output.length - 2]) + (output[output.lenght - 1]));
}
}
return output
}
So when i use function with n=3 and higher, it pushes sum of the two last numbers of output array into that array, till n< output.length. And everything works, loop stops when n=output.lenght, but i got back NaN's, not numbers. What am i doing wrong?
Mispelled length as lenght here
while( n > output.length){
output.push((output[output.length - 2]) + (output[output.lenght - 1]));
}
}
function fibonacciGenerator(n) {
var output = [];
if (n === 2) {
output.push(0, 1);
}
else if (n === 1) {
output.push(0);
}
else {
output = [0, 1];
while (n > output.length) {
// problem was here output.length was misspelld
output.push((output[output.length - 2]) + (output[output.length - 1]));
}
}
return output
}
console.log(fibonacciGenerator(5))

how can I program an Armstrong number in Javascript with one For loop, given that the algorithm used is mathematically correct? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have tried to code this problem by converting the looped variables from 100 to 999 to strings, seperate it's digits to numerical values, find the sum of them when cubed, it seems correct when I calculate it on pen and paper, however when I log it it just prints all looped numbers from 100 to 999 , he is my code
for (var i=100; i<=999; i++){
var x = i.toString();
var z = i
var a = parseInt(x[0]);
var b = parseInt(x[1]);
var c = parseInt(x[2]);
var y = (a * a * a) + (b * b * b) + (c * c * c);
if (y = z){console.log ("a happy number is " + x);}
}
Here is a jsfiddle for you JS FIDDLE LINK
for (var i=100; i<=999; i++){
var myString = i.toString();
var a,b,c;
//this is how I would split string apart
a =myString.substring(0,1);
b =myString.substring(1,2);
c =myString.substring(2,3);
var y = (a * a * a) + (b * b * b) + (c * c * c);
//this was your logic error
if (y === i){console.log ("a happy number is " + i);}
}
console.log('done');

JavaScript code to include Maximum and minimum limit for a number Z [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 8 years ago.
Improve this question
function add(id,value)
{
var x = document. get Element By Id(id). value ;
x = x.replace('$','');
x = x.replace(',','');
var y = value;
var z = +x + +y;
document.get Element By Id(id). value =z;
}
To set a minimum value for z as 0 and maximium value as 999999999
If your question is how to make sure z is never less than 0 or greater than 999999999, there are two common ways:
Using if:
if (z < 0) {
z = 0;
}
else if (z > 999999999) {
z = 999999999;
}
Using Math:
z = Math.max(0, Math.min(z, 999999999));
Math.min(z, 999999999) will pick the smallest of the values you give it, and so won't return a value greater than 999999999. Similarly, Math.max(0, ...) will return the largest of the two values you give it, and so won't return a value less than 0.
An obvious solution to this would be
if (z > 999999999) {
z = 999999999;
}else if (z < 0) {
z = 0;
};
Insert this between var z = +x + +y; and document.get Element By Id(id). value =z;

Categories