Multiplying decimals in Actionscript 3 - actionscript-3

I'm making a tax calculator for my class and I can't figure out how to multiply a whole number with a decimal. I have to multiply the whole by 0.13. Here's my code
var amount:Number;
var hst:int;
amount_txt.restrict = "0-9";
calculate_btn.addEventListener(MouseEvent.CLICK, calculate);
function calculate(event:MouseEvent):void
{
amount = Number(amount_txt.text);
total_txt.text = "You have spent a total of " + String(Math.round((amount * hst)) + "$")
}
I would appreciate help quick, as it is due tomorrow. I apologize if the formatting on here is incorrect, but I assure you on the actual program it is correct. Thanks

The reason is that your HST is not a Number but an int. In order to make it a decimal number you have to change it as a Number
var amount:Number;
var hst:Number;
amount_txt.restrict = "0-9";
calculate_btn.addEventListener(MouseEvent.CLICK, calculate);
function calculate(event:MouseEvent):void
{
amount = Number(amount_txt.text);
total_txt.text = "You have spent a total of " + String(Math.round((amount *hst)) + "$")
}

Related

Undefined result from function (html script)

I'm trying to calculate total pay from inputs given by the user in an html script. I keep getting an undefined result for total pay. I've tried calling the calculate_pay function from the second script tags but that didn't change anything. What am I doing wrong?
<!DOCTYPE html>
<head>
<title>Chapter 4 Assignment</title>
<script>
function input_output(netPay)
{
var hoursWorked = prompt("How many hours have you worked?","");
var payRate = prompt ("What is your hourly pay rate?","");
var taxRate = prompt ("What is the tax rate for your state? ex. 11 for 11%","");
var br = "<br>";
document.write("Hours worked: "+hoursWorked+br);
document.write("Pay rate: "+payRate+br);
document.write("Tax rate: "+taxRate+br);
document.write("Total pay: "+netPay+br);
}
function calculate_pay(hoursWorked,payRate,taxRate)
{
var taxRate = taxRate/100;
var grossPay = (hoursWorked*taxRate);
var netPay = grossPay - (taxRate * grossPay);
return netPay;
}
</script>
</head>
<body>
<script>
input_output();
</script>
</body>
</html>
I've tried calling the calculate_pay function in input_output like this:
function input_output(netPay)
{
var hoursWorked = prompt("How many hours have you worked?","");
var payRate = prompt ("What is your hourly pay rate?","");
var taxRate = prompt ("What is the tax rate for your state? ex. 11 for 11%","");
var br = "<br>";
calculate_pay();
document.write("Hours worked: "+hoursWorked+br);
document.write("Pay rate: "+payRate+br);
document.write("Tax rate: "+taxRate+br);
document.write("Total pay: "+netPay+br);
}
As well as calling it in the second script tags and both at the same time but it still returns undefined.
You are only calling the input_output() function, which prompts the user for hoursWorked, payRate, and taxRate, but never calculates anything. The parameter to input_output(), netPay, is probably misplaced. You should call your function calculate_pay() to actually get that value.
The last line in your first function should be
document.write("Total pay: " + calculate_pay(hoursWorked,payRate,taxRate) + br);
But before even getting there, you must take care to convert the input values from String to a Number using parseFloat. That is, for each prompt that you have, do the following:
var hoursWorked = parseFloat(
prompt("How many hours have you worked?","")
);
Whatever the user types will always be a String, so always remember to convert those string values to an actual Number using the parseFloat() method.
I think you want something like this instead:
function input_output() {...} // get rid of the input parameter
Add this after the var taxRate line:
var netPay = calculate_pay(hoursWorked, payRate, taxRate);

Is their a simple way to detect if the actual time is near a specific time?

I've got an AS3 code that calculate and displays 4 tides.
Example :
High 1 = 13:20
High 2 = 23:30
Low 1 = 05:30
Low 2 = 16:20
In code, I'm not gonna put here the calculation as it's too long and not necessary for this problem, but the variables are :
var $pmm:String;
var $pms:String;
var $bmm:String;
var $bms:String;
High 1 = $pmm
High 2 = $pms
Low 1 = $bmm
Low 2 = $bms
The app also displays the actual time
var my_timer:Timer=new Timer(1000);
my_timer.addEventListener(TimerEvent.TIMER, onTimer);
my_timer.start();
function onTimer(e:TimerEvent):void {
now = new Date();
trace(now.hours + ":" + now.minutes);
}
actual_time.text=now.hours + ":" + now.minutes;
Is there a simple way to detect if now.hours is closest to $pmm, $pms, $bmm or $bms ?
Not sure if this would be the best method, but I would take the difference between the now time and the tide times and push it into an array like so:
var tideArray = new Array();
tideArray.push({tide:"pmm", difference: 3});
tideArray.push({tide:"pms", difference: 5});
tideArray.push({tide:"bmm", difference: 0});
tideArray.push({tide:"bms", difference: 8});
tideArray.sortOn("difference", Array.NUMERIC);
trace(tideArray[0].tide);
The trace of the first element would return the lowest difference in time, which in this case would be bmm.

Is it possible to convert milliseconds to normal time hh:mm:ss without Javascript?

Currently I have a form with checkboxes and a table with several outcomes. This all works great with a Javascript. But in order to use this savely on a forum it is better to not use Javascript as these are used inside postings.
At the moment this form has several checkable options for calculating factors to decrease a standard milliseconds time, and output them in a result.
Now I want to rebuild it to not use Javascript. And that should works for above options. But those milliseconds have to show up in the end as hours : minutes : seconds.
Is this possible to achive without the use of the Java function? (and without php) So HTML or CSS only.
I already have looked in several things, like the pure CSS calculator etc, but those are all numbers only. I need a time convertion.
Currently I use this functions code
// function milliseconds to time
function msTime(s) {
var ms = s % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;
mins = ("00" + mins).substr(-2);
secs = ("00" + secs).substr(-2);
return ' ' + hrs + 'h ' + ' ' + mins + 'm ' + ' ' + secs + 's';
}
It is not possible without Javascript. you can simplify the function as follows.
var secondsToTime = function(duration) {
var date = new Date(duration);
return "%hours:%minutes:%seconds"
.replace('%hours', date.getHours())
.replace('%minutes', date.getMinutes())
.replace('%seconds', date.getSeconds());
}
console.log(secondsToTime(1324339210310));

Flash sum total loosing trailing zeros

I have a sum that works out any chosen percentage discount (percentage1.text) of a selected product (status2_txt.text). I've managed to get the value to display just 2 decimal points but it looses its trailing zeros, pulling my hair out!
This is what I currently have:
total_btn3.addEventListener(MouseEvent.CLICK, getTotal3);
function getTotal3(e:MouseEvent):void {
discount1.text = String( Number(status2_txt.text) / Number(100) * Number(percentage1.text));
discount1.text = int((discount1.text)*100)/100;
}
I'd appreciate any help with this!
Thanks
Try function toFixed.
var ret:Number = Number(status2_txt.text) / 100 * Number(percentage1.text);
discount1.text = ret.toFixed(2);

ActionScript 3: dynamic text help: scoring for game

I am very new to action script 3, and I am trying to make a very basic game right now. However, no matter how many pages I look at I can't find a working way to get my game to keep score :/.
What I am trying to do is make it so that every 10 seconds, 10 points is added to the score (right now I have it replaces with a key, to see if I could get that to work, but it didn't).
This is the code I am trying to use right now:
var playerScore:int = 0
stage.addEventListener(MouseEvent.CLICK,onclick);
function updateTextFields():void{
playerScoreText.text = ("Player Score: " + playerScore);
}
if(Key.isDown(Key.G)){
playerScore++; //increase playerScore by 1
updateTextFields();
}
playerScoreText is the name of the dynamic text
any help will be greatly appreciated :)
This code was all added in Timeline
I am thinking the problem is most likely something with the creation of the dynamic text, but I am not sure.
Make sure the fonts are embedded properly and that the color of the dynamic text field is not same as the background.
also instead of writing
playerScoreText.text = ("Player Score: " + playerScore);
try this
playerScoreText.text = "Player Score: " + String(playerScore);
It sounds like you want to do something like this with the timer class. Your key code isn't written properly.
var playerScore:int = 0;
var score_timer:Timer = new Timer(10000,0);
score_timer.addEventListener(TimerEvent.TIMER,updateTextFields);
score_timer.start();
function updateTextFields(e:TimerEvent):void
{
playerScore+=10
playerScoreText.text = ("Player Score: " + playerScore);
}