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 8 years ago.
Improve this question
The issue I am having is I'm trying to get the alert to tell the person if the random number generated is in the ranges to be an A,B,C,D, or F. All that happens is it says all the numbers are F's not matter what.
var grade = (Math.floor(Math.random() * 100 + 1));
document.write(Math.floor(Math.random() * 100 + 1))
if (grade >= 90) {
alert("A");
} else if (grade >= 80) {
alert("B");
} else if (grade >= 70) {
alert("C");
} else if (grade >= 60) {
alert("D");
} else {
alert("F");
}
The problem is that you're checking a different number than you're writing with document.write here:
var grade = (Math.floor(Math.random() * 100 + 1));
document.write(Math.floor(Math.random() * 100 + 1))
This would need to be:
var grade = (Math.floor(Math.random() * 100 + 1));
document.write(grade);
In order to accurately reflect what is happening.
The problem is that your not generating a random value properly.
Math.random() returns a random number between 0 (inclusive) and 1 (exclusive). Try using this statement.
var grade = Math.floor((Math.random() * 100));
Related
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
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 tried a few ways to get a random number from 1 to 10 and all return undefined or NaN why ?
here is what i tried
var num = Math.floor(Math.random * 10)
function getNum() {
return Math.floor(Math.random * 10);
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
both dosn't give a number when logged
You need to actually invoke Math.random if you intend for it to generate the random number (ie Math.random())
var num = Math.floor(Math.random() * 10)
function getNum() {
return Math.floor(Math.random() * 10);
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
Math.random is a method, not a property, so you need to call it as Math.random(). You are also missing a semicolon after your first line.
As stated by #kloddant Math.random is method so you forgot the parentheses ().
So here's the snippet of how you can implement it
var num = Math.floor(Math.random() * Math.floor(10))
function getNum() {
return Math.floor(Math.random() * Math.floor(10));
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
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
I am trying to create code that if we generate number between 20 and 120 and the generated number is >= than 100 then we want to reduce by 20. I dont want to create multiple random numbers..Can someone help ??
my basic math.rand function -
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
the code i tried to make
randomNumber(20,120);
if(randomNumber >= 100){
return randomNumber - 20;
}
message.reply(randomNumber);
In messages it display function randomNumber(min, max) { etc...
Store the result of the randomNumber function in a variable like this:
let number = randomNumber(20,120);
if(number >= 100){
number -= 20;
}
message.reply(number);
In JavaScript, you add () to run a function. Without those parentheses, you are just returning the code itself. When you state if(randomNumber >= 100), you aren't comparing the value that the function returns, rather the function itself.
When you call randomNumber(20,120) it returns a number, but you don’t put that number in any variable, so it just gets lost. And then you return randomNumber, which is a function, that’s why it prints out the content of the function.
To avoid that, create a variable containing the random number, like so:
let x = randomNumber(20,120);
if(x >= 100){
x -= 20;
}
message.reply(x);
Just save the result of your function in a variable and then check if that variable is >= 100 and if it is reduce it by 20 using a simple ternary operator.
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
number = randomNumber(20,120);
number = (number < 100) ? number : number -20;
console.log(number);
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 4 years ago.
Improve this question
var allBills = [124,48,268,180];
var tips =[];
//var total = [];
document.write(allBills);
function calcIt(bill){
for (i=0; i < bill.length; i++){
if(bill[i] < 50){
tips[i] = bill[i] * 20% + " ";
}else if(bill > 50 && bill <200){
tips[i] = bill[i] * 15% + " ";
}else {
tips[i] = bill[i] * 10% + " ";
}
return tips += tips[i];
};
document.write(calcIt(allBills));
I am trying to multiply for each restaurant bill var allBills = [124,48,268,180]; , Why is this not working?
For percentages, you can either divide by 100 or simply use percentage as follow: 100 = 1, 90 = .9, 50 = .5, 15 = .15, 5 = .05, and so on.
Wrap the calculation with parentheses to avoid any clash with the strings.
I used toFixed(2) just for readability
var allBills = [124, 48, 268, 180];
function calcIt(bill) {
var tips = [];
for (i = 0; i < bill.length; i++) {
if (bill[i] < 50) tips[i] = (bill[i] * .2).toFixed(2) + " ";
else if (bill > 50 && bill < 200) tips[i] = (bill[i] * .15).toFixed(2) + " ";
else tips[i] = (bill[i] * .1).toFixed(2) + " ";
}
return tips;
}
document.write(calcIt(allBills).join(" | "));
You are using reminder operator (%) , which provides the reminder. Your logic does not fit with what you are expecting. If you want to add the % symbol to your out put then use it as a string
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