Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ryanosten
PHP Development Techdegree Student 29,615 Pointswhy don't we have to define player?
Question - in the test suite, we make a player object to use in expectation. Im curious why we do not have to define it as a variable? like 'const player: {}'
const expect = require('chai').expect;
describe('checkForShip', function(){ const checkForShip = require('../game_logic/ship_methods').checkForShip;
it('should correctly report no ship at a given players coordinate', function(){
player = {
ships: [
{
locations: [[0,0]]
}
]
}
expect(checkForShip(player, [9, 9])).to.be.false;
});
1 Answer
Rune Andreas Nielsen
5,354 PointsHi, Ryanosten.
In JavaScript, if you're not running in 'strict mode', you don't have to declare a variable. The reason is that, if you don't declare it, the JavaScript engine will automatically declare it for you.
If you instead insert 'use strict' in the beginning of your file, the compiler will throw an error if it finds a variable that is not declared.
Note that it is always best that you declare the variables yourself.
If you want more information, you can check out a blog post that I wrote about Scope in JavaScript where I explain why the variables get declared for you, if not in strict mode http://www.constructcode.com/post/javascript-understanding-scope.