Very simple SD function [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
var orangeCost = function (pricePerOrange, amountOfOranges) {
var totalCost = pricePerOrange * amountOfOranges;
console.log(totalCost + " Dollar total cost");
};
orangeCost(5, 5);
25 Dollar total cost
NaN Dollar total cost
Why does it also log the "NaN" part instead of only the 25 Dollar stuff ?
thank you

The only way a multiplication could result in NaN is if the variables were undefined, meaning you're calling the function without passing pricePerOrange or amountOfOranges, or if one of the variables is a string.
Make sure you're passing variables to orangeCost, and that those variables are in fact Numbers.

Related

turn money amount to symbol equivalent [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 us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Improve this question
I am searching for a method to turn a numeric value such as $10.00 into a symbol.
I need to have symbols to represent the price of a product.
A good example is:
http://www.customink.com/categories/short-sleeve-t-shirts/16/styles#facets/
Instead of displaying prices they show currency symbols. So the price of a cheap item is represented by $ while a more expensive item is $$$
For the code it needs to:
For a value between $1-$10.99 to display it as $
For a value between $11-$25.99 to display it as $$
For a value between $26-$200 to display it as $$$
Thank you, for any guidance you can provide.
What have you tried?
var symbol;
if( value < 11.0 ) {
symbol = "$";
}
else if( value < 26.0 ) {
symbol = "$$";
}
else {
symbol = "$$$";
}
return symbol
Or am I missing something here? There are plenty of more sophisticated approaches, but if those are actually your requirements what is wrong with an if/else clause?

Randomly generate a number in Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I want to randomly generate a number between 1 and 10 and also display it on a webpage, does anyone have any idea how i'd go about doing this?
preferably in full since i have no idea what im doing
C'mon man. Google something. jsfiddle
var min = 1, max = 10,
num = Math.floor(Math.random() * (max - min + 1)) + min;
document.body.innerHTML = num;

Javascript, adding to a variable not setting [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
I'm having trouble adding to a var at the moment I can only get it to set, I tried a number of operators like "+=" "++" but it doesn't work :(
I have a function that gets called a number of times inside I have this
score = Math.floor((Math.random()*15)*1);
But when it gets activated the function runs and sets the score then when its run again it overwrites the old score with the new random number, I want to add the old score plus whatever is generated together do I need a 2nd var?
When I tried to use score += Math.floor((Math.random()*15)*1); I got the following error:
Uncaught ReferenceError: score is not defined
Let me take a guess, score was not declared to be an Int but a string
try
var score = 0;
score += Math.floor((Math.random()*15)*1);
The issue is that score += Math.floor((Math.random()*15)*1); really means score = score + Math.floor((Math.random()*15)*1);, but, since you haven't defined score anywhere yet, when it tries to use it in the calculation, it is "undefined" (and tells you as much :D ).
Define score before attempting to use the += operator on it and you should be fine.

Write new line adding +1 to base value until total value = 10? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm currently starting to learn Javascript and would be grateful for some help with the following issue:
I'm trying to make a loop that writes a new line with a base value that adds by 1 with each line until the value is equal to 10. Basically, it's supposed to look like this:
Answer: 1
Answer: 2
Answer: 3
... and so on, until it reaches 10. There' the loop should end.
How do I achieve this?
Easy solution. Do not use in anything serious:
for (x=1;x<11;x++)
{
document.write("Answer: "+x+"<br />");
}
More versatile solution with DOM (fiddle):
<p id="numbers">
</p>
<script type="text/javascript">
for (x=1;x<11;x++)
{
document.getElementById("numbers").innerHTML+="Answer: "+x+"<br />"
}
</script>
Use a for loop like so:
for(var x = 1; x <= 10; x++) //start x as equal to one, run the code, and repeat until x is 10
console.log("Answer: " + x);//Print x

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