These are chat archives for codefellows/sea-f1-TAs/Brian

21st
Sep 2014
Brian Caldwell
@Caldwerl
Sep 21 2014 00:50
You should have all of your JS inside that
Matthew Conquergood
@kristjin
Sep 21 2014 00:51
variable declarations, functions, constructors, the whole lot.
Brian Caldwell
@Caldwerl
Sep 21 2014 00:51
If youve written any other programs, the document ready thing is essentially your main function
Ah yes, not those
var declarations will go there
along with function calls and the like
function declarations and constructors do not need to be in that code block
Matthew Conquergood
@kristjin
Sep 21 2014 00:52
Got it.
Just the 'active' parts
Brian Caldwell
@Caldwerl
Sep 21 2014 00:52
Yes
Matthew Conquergood
@kristjin
Sep 21 2014 00:53
and the value of putting my variable declarations inside the ready function is, in part, that they become localized to that script only, right?
Brian Caldwell
@Caldwerl
Sep 21 2014 00:53
The value is that they fire when the document is ready and not a second earlier
because many times those values will be DOM elements or the like
Matthew Conquergood
@kristjin
Sep 21 2014 00:54
Right... is it also true that they are local to that script only, in scope?
Brian Caldwell
@Caldwerl
Sep 21 2014 00:55
It is a function essentially so yes
Matthew Conquergood
@kristjin
Sep 21 2014 00:56
I just have this getRandomInt() function and I'm wondering if it matters if that is declared as var getRandomInt = function(... or function getRandomInt(...
Brian Caldwell
@Caldwerl
Sep 21 2014 00:58
if you use the var route, it will have to be above the code it is used in
if you use the function route
it will be hoisted by JS
Matthew Conquergood
@kristjin
Sep 21 2014 00:58
I probably won't want to depend on that
Brian Caldwell
@Caldwerl
Sep 21 2014 00:58
so it will be able to see it everywhere
Matthew Conquergood
@kristjin
Sep 21 2014 00:58
hoisting is cool but makes it hard to find stuff
Brian Caldwell
@Caldwerl
Sep 21 2014 00:59
Not if you declare all your functions like that in one area
Matthew Conquergood
@kristjin
Sep 21 2014 00:59
Well that's what I mean. I do that at the very top.
First thing.
Brian Caldwell
@Caldwerl
Sep 21 2014 00:59
Then either way works
Matthew Conquergood
@kristjin
Sep 21 2014 00:59
So it's always above everything else. Yeah.
Brian Caldwell
@Caldwerl
Sep 21 2014 01:00
I learned with C where it was expected that functions be written under main
so im used to hoisting
but whatever works for you
Matthew Conquergood
@kristjin
Sep 21 2014 01:00
I've been putting a lot of effort into writing something that's just really easy to read and navigate.
That's why I put it up top but if there's a more appropriate convention I'd rather follow that.
Brian Caldwell
@Caldwerl
Sep 21 2014 01:01
So long as its uniform throughout it shouldnt be a problem
Matthew Conquergood
@kristjin
Sep 21 2014 01:01
Right on.
Brian Caldwell
@Caldwerl
Sep 21 2014 01:01
Just dont mix and match
Matthew Conquergood
@kristjin
Sep 21 2014 01:01
roger that
rlwismans
@rlwismans
Sep 21 2014 03:59

function Animal(name, speed, focus, type) {
this.name = name;
this.speed = speed;
this.focus = focus; //values 0-9
this.type = type
this.position = 0;
this.isFocused = function() {
return Math.floor(Math.random() * 10) < this.focus;
}
this.advance = function() {
if (this.isFocused()) {
this.position += this.speed;
}
}
this.progressReport = function() {
return this.name + " the " + this.type + " is at: " + this.position;
}
}

(function(){
var form =document.getElementById('newUserCreature');

$('#userImput.on')('submit', function (e) {
e.preventDefault();
var userCreature = $('#userCreature').val();
var userName = $('#userName').val();
var userFast = $('#userSpeed').val();
var userFocus = $('#userFocus').val();
var msg = 'Fantastic, ' + userName + 'the ' + userCreature+' will be racing against Pandora the rabbit and Penelope the turtle today!';
$('#main').replaceWith('<p id="main">' + msg + '</p>');
});

//create rabbit
var rabbit = new Animal('Pandora', 8, 2, 'Rabbit');

//Create turtle
var turtle = new Animal('Penelope', 3, 8, 'Turtle');

//Create userAnimal
var userAnimal = new Animal(userName, parseInt(userSpeed), parseInt(userFocus), userCreature);

//run the race

var meters = 100;

while (rabbit.position < meters && turtle.position < meters && userAnimal1.position < meters) {
rabbit.advance();
turtle.advance();
userAnimal1.advance();
$document.write(
rabbit.progressReport()<br>
turtle.progressReport()<br>
userAnimal.progressReport());
}
});