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

*book if you are*

**free****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 *

## Leave a Reply

15 Comments on "Developer Basics and Math Challenge"

`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;

}

I am so glad to see your edit.. I tried so hard to use the maxValue instead of creating the variable highGrade.

What I still don’t get is the why to add the “return”.. and it only works when the return is the very last item…?

ohh thats a very importent question que the questions up and we will go over it in our next live session just tell me when you are ready. (not this sunday but we should make another private session talk about functions and then move on to loops part 2).

`///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.

this is very nice to make it more of a utility function lets just change a few small things by getting rid of the event and instead doing this:

by the way i know its the exact same code just condensed and now only one display object and the 2 other params are the x,y to look at:

import flash.display.DisplayObject;

public function rotateToPoint(dis:DisplayObject,_x:int,_y:int):void{

dis.rotation = Math.atan2(_x â dis.x,_y â dis.y) * 180 / Math.PI;

}

very nice!

`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;

}

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!

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`

Rock star i totally love it. now we have a way to get quick discounts for products. very useful and mathematical. added to the list. and adding 2 bonus points to your account.

function realRandom(low:Number=0, high:Number=1):Number {

return Math.floor(Math.random() * (1+high-low)) + low;

}

very nice! cool function now only catch is the name the word real is used in path as a real number (a number that is between 0-1 such as 3.23212312 while you mean to have an int as only integers would come back so i would change the name of the function and the variable types to be :

`function intRandom(low:int=0, high:int=1):int {`

return Math.floor(Math.random() * (1+high-low)) + low;

}

`trace(intRandom(0,100));`

but great added to the list

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)

ha good. please take a look at the usage of for each ( i just changed your for into for each just for practice so we can see all the various loop types) .