Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some bugs, suggest a wording change #1

Open
wants to merge 3 commits into
base: full-time
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
585 changes: 575 additions & 10 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"chai": "^4.3.10",
"chai-spies": "^1.0.0",
"mocha": "^10.2.0"
},
"devDependencies": {
"@babel/core": "^7.25.2"
}
}
13 changes: 9 additions & 4 deletions problems/01-declare-key-values.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/*
Given the following declaration of an object, `obj` below, declare values so
that the print statements output what is expected. Try using both dot and
bracket notation.
Given the empty object `obj` declared below, add properties to it so
that the console.log statements output the expected values.

Ensure that each property has the correct value type, not just a
value that prints correctly. For example, the boolean property
should be set to the boolean value `false`, not the string "false".

Use both dot and bracket notation to add properties.
*/

const obj = {};

/******************** DO NOT MODIFY ANY CODE ABOVE THIS LINE *****************/

// Your code here
// Your code here

/******************** DO NOT MODIFY ANY CODE BELOW THIS LINE *****************/

Expand Down
2 changes: 1 addition & 1 deletion problems/02-key-deletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const obj = {
}
/******************** DO NOT MODIFY ANY CODE ABOVE THIS LINE *****************/

// Your code here
// Your code here

/******************** DO NOT MODIFY ANY CODE BELOW THIS LINE *****************/

Expand Down
2 changes: 1 addition & 1 deletion problems/03-variable-as-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ string parameter and returns the value from the object at the key of the string
passed in.
*/

// Your code here
// Your code here

// Example:
// const obj = {
Expand Down
2 changes: 1 addition & 1 deletion problems/04-cat-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object with a key of "name", "color", and "toys" that correspond to the
arguments passed in.
*/

// Your code here
// Your code here

// console.log(catBuilder("Whiskers", "black", ["scratching-post", "yarn"]));
// //=> { name: 'Whiskers', color: 'black', toys: ['scratching-post', 'yarn'] }
Expand Down
2 changes: 1 addition & 1 deletion problems/05-print-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Write a function called printNames that takes in an array of objects. It should
loop through all of the objects and print the "name" key from each object.
*/

// Your code here
// Your code here

// Example:
// printNames([
Expand Down
2 changes: 1 addition & 1 deletion problems/06-has-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ strings. It should return true if all of the strings in the array are keys in
the object.
*/

// Your code here
// Your code here

// let survey = {
// name: 'Check',
Expand Down
48 changes: 38 additions & 10 deletions test/01-declare-key-values-spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
const chai = require("chai");
chai.use(require("chai-spies"));
const expect = chai.expect;
const fs = require('fs');
const path = require('path');
const babel = require('@babel/core');

describe("01-declare-key-values.js", function() {
let consoleSpy;
let obj;
before(() => {
consoleSpy = chai.spy.on(console, 'log');
obj = require("../problems/01-declare-key-values");
});

after(() => {
chai.spy.restore(console);
});
let consoleSpy = chai.spy.on(console, 'log');
let filePath = path.resolve(__dirname, '../problems/01-declare-key-values.js');
let obj = require(filePath);

it("prints 'firstValue' first", function () {
expect(consoleSpy).on.nth(1).be.called.with('firstValue');
Expand All @@ -30,4 +26,36 @@ describe("01-declare-key-values.js", function() {
expect(consoleSpy).on.nth(4).be.called.with({ hello: "world!" });
expect(obj["object"]).to.eql({ hello: "world!" });
});
it('uses both dot and bracket notation', function () {
let code = fs.readFileSync(filePath, 'utf-8');
let usesDotNotation = false;
let usesBracketNotation = false;

babel.parse(code, { sourceType: 'module' }, (error, ast) => {
if (error) {
console.error(`error parsing ${filePath}`);
console.error(error.message || 'Unknown error');
return;
}

babel.traverse(ast, {
AssignmentExpression(path) {
let target = path.node.left;
if (target.type === 'MemberExpression' &&
target.object.name === 'obj') {
if (target.computed) {
usesBracketNotation = true;
} else {
usesDotNotation = true;
}
}
}
});
});

expect(usesDotNotation, 'uses dot notation').to.be.true;
expect(usesBracketNotation, 'uses bracket notation').to.be.true;
});

chai.spy.restore(console);
});