Developer Basics and Math Challenge

Ready for a fun challenge? Its all about helping you ease into becoming that amazing developer that we all know you are. Well it's simple. Help us create together a nice library of math and math related functions (based on our Developer Basics classes).

Ready for a fun challenge? Its all about helping you ease into becoming that amazing developer that we all know you are. Well it’s simple. Help us create together a nice library of math and math related functions (based on our Developer Basics classes). Every member will win some points. If we reach our 25 useful functions we will raffle a book from our goodie bag.

Task: create a function that is inspired by the Math class (in other words recreate the Math functions or come up with other useful mathematical functions that aren’t part of the math class).

Rules:

1) Every answer needs to be unique and needs to work in the real world.
2) You need to understand what you pasted in 😛 – I’ll check!
3) You can submit as many answers as you want – only rule is you can’t do it all at one time
– you must wait for one other person to submit an answer before posting a new post for it to be counted in.

What you will get:

– just for posting something correct 2 bonus points (that is if it wasn’t done already before you).
– if we get more than 25 correct answers we will raffle a free month on our site/ or a free book if you are one of us already.

To take part just replay this thread with your answer (no duplicates but you can do the same Math function if you have a unique way that wasn’t covered yet up here). Have FUN !

Functions created already:

1) deleteHigh – Finds the highest number in an array and extracts it from it and returns it to the caller (Carole +2 bonus points).
2) rotateToPoint – rotates display objects to look at random points on the screen(Adam +2 bonus points).
3) discount calculator – set a price and discount and get the product price(Ramez +2 bonus points)
4) intRandom – you set a range the function returns an integer between these values (Michael 2 Points)
5) average -input an array and get the average value. (Carole now at 4 extra points).

6) … it could be you …

Current Score Board:
Carole – 4 Points
Ramez – 2 Points
Adam – 2 Points
Michael – 2 Points

Ideas for other functions that could be created:
Math.random, Math.abs, Math.ceil, Math.floor, Math.round … and more

– go from a “zero to geek” | 02Geek.com – ©

Leave a Reply

15 Comments on "Developer Basics and Math Challenge"

Notify of
avatar

Carole van Almelo
Guest
Carole van Almelo
5 years 6 months ago

var aGrades:Array = new Array(85, 85, 80,30,90);
findDeleteHigh(aGrades);

/*find and delete highest grade*/
function findDeleteHigh(a:Array):Number{
var maxValue:Number = Number.MIN_VALUE;
var highGrade:int = maxValue;
var length:int = a.length;

for(var i:int=0; i<length;i++){
if(maxValue<a[i]) maxValue = a[i];
}
highGrade = maxValue;
trace ("The highest grade is " + highGrade + ".");
trace ("--------------------------------")

var highPosition:int = aGrades.indexOf(highGrade);
trace ("This high grade in array position of " + highPosition + ".");
trace ("--------------------------------")
aGrades.splice(highPosition, 1);
trace ("When high grade is delete, the remaining grades are " + aGrades + ".");
trace ("--------------------------------")
return maxValue;
}

Adam Scott
Guest
Adam Scott
5 years 6 months ago

///Mouse Event - Can be used for any type of event using Math.atan2!
public function rotateTo(event:Event):void{
var dx:Number = objectToFollow.x - objectFollowing.x;
var dy:Number = objectToFollow.y - objectFollowing.y;
var aRotation:Number = Math.atan2(dy,dx);

objectFollowing.rotation = aRotation * 180 / Math.PI;

}
This is something I used early on when moving that bunny around the screen.

Carole van Almelo
Guest
Carole van Almelo
5 years 6 months ago

function averageGrades(a:Array):Number{
var length:int = a.length;
var sum:int = 0;
var average:int = 0;

for(var i:int=0; i= 90){
trace("Grade: A.");
}else if (average >= 80){
trace("Grade: B.");
}else if (average >= 70){
trace("Grade: C.");
}else if (average >= 60){
trace("Grade: D.");
}else{
trace("Grade: F.");
}
return sum;
}

Ben Fhala
Guest
5 years 6 months ago

I love the idea but right now it doesn’t do the trick so it doesn’t pass yet but make it work! this is a great function just get rid of grades just make it a simple function that takes in an array of numbers and returns an average number. that way we can use it i might even use it in one of our next classes if you get it up right! if you have any issue with it feel free always to drop a line but i know you can make it as your last function is more complex!

Ramez
Guest
Ramez
5 years 6 months ago

A function to get the final price after being discounted and how much discount you get(if your shopping);

package
{

public class Price
{
private var _finalPrice:Number;
private var _discount:Number;

public function Price(price:Number=0, percent:Number=0)
{
getPrice(price,percent);
}

private function getPrice(price:Number, percent:Number):void
{
_finalPrice = price * (100- percent)/100
_discount = price - _finalPrice;

}

public function get finalPrice():Number
{
return _finalPrice;
}

public function get discount():Number
{
return _discount;
}

}
}

in a new doc instantiate Price

var price:Price = new Price(100,10)

trace(price.finalPrice,price.discount) //90, 10

Michael
Guest
Michael
5 years 6 months ago

function realRandom(low:Number=0, high:Number=1):Number {
return Math.floor(Math.random() * (1+high-low)) + low;
}

Carole van Almelo
Guest
Carole van Almelo
5 years 6 months ago

function avrg(a:Array):Number{
var sum:Number=0;
for each(var i:int in a){
sum+=i;
}
return sum/a.length;
}

trace(avrg([0,0,0,100,100,100]));

(edited and grouped up together , Carole i hope you don’t mind i changed a few small things)

Michael
Guest
Michael
5 years 6 months ago
just came up with this for my current project. Find out if a date is outdated = true or otherwise false. Usable for EU/US date formats and easily extendable for others. public static const EU_DATE_FORMAT:String = "eu:format"; // dd.mm.yyyy (europe) public static const US_DATE_FORMAT:String = "us:format"; // mm/dd/yyyy (usa) public static function isOutdated(testDate:String, format:String = EU_DATE_FORMAT, delimiter:String = "."):Boolean { var day:uint = new Date().date, month:uint = new Date().month + 1, year:uint = new Date().fullYear; // current Date var d:uint, m:uint, y:uint; // element pos for date format var arr:Array = testDate.split(delimiter); if (arr.length != 3) throw new ArgumentError("invalid Date… Read more »
wpDiscuz