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 trialFaddah Wolf
12,811 Pointsutterly lost - get "unexpected token" error of the ')' on ship_methods.js line 35, but there is _no_ line 35!
hello,
i'm sorry, but i'm quite lost at this point. i've tried to follow along in the video and copy the code exactly, but i am getting this error i don't even understand when trying to run npm test
at this point in the training moduel —
treehouse:~/workspace$ npm test
> workspace@1.0.0 test /home/treehouse/workspace
> mocha
/home/treehouse/workspace/game_logic/ship_methods.js:35
});
^
SyntaxError: Unexpected token )
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Suite.<anonymous> (/home/treehouse/workspace/test/ship_test.js:4:21)
at context.describe.context.context (/home/treehouse/workspace/node_modules/mocha/lib/inte
rfaces/bdd.js:47:10)
at Object.<anonymous> (/home/treehouse/workspace/test/ship_test.js:3:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at /home/treehouse/workspace/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/home/treehouse/workspace/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/home/treehouse/workspace/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/home/treehouse/workspace/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
npm ERR! Test failed. See above for more details.
... it says the ')' token is bad on line 35 of ship_methods.js, but there is no line 35. it ends on line 34.
here are my ship_methods.js, main_test.js & and ship_test.js files at this point. i called my method for determining when a player attacks a ship shipAttack()
instead of fire()
, but that should not make any difference —
ship_methods.js —
function checkForShip(player, coordinates) {
var shipPresent, ship;
for(var i = 0; i < player.ships.length; i++) {
ship = player.ships[i];
shipPresent = ship.locations.filter(function(actualCoordinate) {
return (actualCoordinate[0] === coordinates[0]) && (actualCoordinate[1] === coordinates[1]);
})[0];
if(shipPresent) {
return ship;
}
}
return false;
}
function damageShip(player, coordinates) {
ship.damage.push(coordinates);
}
function attackShip(player, coordinates) {
/* if(damageShip(ship, coordinates)) {
return true;
} */
var ship = checkForShip(player, coordinates);
if(ship) {
damageShip(ship, coordinates);
}
module.exports.checkForShip = checkForShip;
module.exports.damageShip = damageShip;
module.exports.attackShip = attackShip;
main_test.js —
var expect = require('chai').expect;
// Test suite
describe('Mocha', function() {
// Test spec (unit test)
it('should run our tests using npm', function() {
expect(true).to.be.ok;
});
});
ship_test.js —
var expect = require('chai').expect;
describe('checkForShip', function() {
var 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, [0, 0])).to.deep.equal(player.ships[0]);
});
it('should correctly report a ship at a given players coordinate', function() {
player = {
ships: [
{
locations: [[0, 0]]
}
]
};
expect(checkForShip(player, [0,0])).to.be.true;
});
it('should handle ships located at more than one coordinate', function() {
player = {
ships: [
{
locations: [[0, 0], [0, 1]]
}
]
};
expect(checkForShip(player, [0, 1])).to.be.true;
expect(checkForShip(player, [0, 0])).to.be.true;
expect(checkForShip(player, [9, 9])).to.be.false;
});
it('should handle checking multiple ships', function() {
player = {
ships: [
{
locations: [[0, 0], [0, 1]]
},
{
locations: [[1, 0], [1, 1]]
},
{
locations: [[2, 0], [2, 1], [2, 2], [2, 3]]
}
]
};
expect(checkForShip(player, [0, 1])).to.be.true;
expect(checkForShip(player, [0, 0])).to.be.true;
expect(checkForShip(player, [1, 0])).to.be.true;
expect(checkForShip(player, [1, 1])).to.be.true;
expect(checkForShip(player, [2, 3])).to.be.true;
expect(checkForShip(player, [9, 9])).to.be.false;
});
});
describe('damageShip', function() {
var damageShip = require('../game_logic/ship_methods').damageShip;
it('should register damage on a given ship at a given location', function() {
var ship = {
locations: [[0, 0]],
damage: []
};
damageShip(ship, [0, 0]);
expect(ship.damage).to.not.be.empty;
expect(ship.damage[0]).to.deep.equal([0, 0]);
});
});
describe('attackShip', function() {
var attackShip = require('../game_logic/ship_methods').attackShip;
it('should record damage on the given players ship at a given coordinate', function() {
var player= {
ships: [
{
locations: [[0, 0]],
damage: []
}
]
};
attackShip(player, [0, 0]);
expect(player.ships[0].damage[0]).to.be.equal([0, 0]);
});
});
any help anyone, Guil Hernandez, Joseph Fraley or anyone else, could please give, would be appreciated.
best,
— faddah portland, oregon, u.s.a.
3 Answers
Seth Kroger
56,413 PointsLooks like you forgot an curly brace on attactShip() and the interpreter thought the function continued past the script. The way JavaScript loads the scripts loads script files is essentially to concatenate them into one large script, which can cause odd errors when braces aren't matched and such. It finally ran into the error caused by the mismatch somewhere else, but thought it was still in the ship_methods.js file.
Joseph Fraley
Treehouse Guest TeacherHey @Faddah Wolf, it looks like your test for attackShip
is failing due to deep equality.
You have:
expect(player.ships[0].damage[0]).to.be.equal([0, 0]);
But in JavaScript, two arrays with identical vaues are not equal. [0, 0] !== [0, 0]
equality in JS works by reference. Thus:
var a = [0]
var b = a
a === b // <- true
because b
IS ONE AND THE SAME ARRAY as a
. a
and b
are just different names for the one unique object. but:
var a = [0]
var b = [0]
a === b // <- false
a
and b
look the same, but they are two unique arrays with all the same values.
Try this test instead:
expect(player.ships[0].damage[0]).to.deep.equal([0, 0]);
Faddah Wolf
12,811 Pointsah, that was it. i'll mark this as solved. thank you. now if i can just figure out why my shipAttack()
chai tests are not working, that'd be great...