Last active
August 15, 2020 19:02
-
-
Save schalkneethling/54d368363dfc75cd1e0633eae0fc565f to your computer and use it in GitHub Desktop.
What is the output in index.js?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const { update } = require("./util"); | |
const entries = ["one", "two"]; | |
function doUpdate() { | |
let fileContents = fs.readFileSync("./sample.json", "utf8"); | |
fileContents = update(fileContents, entries); | |
console.log(entries); // what will this output? | |
} | |
doUpdate(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"entries": [] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function update(fileContents, entries) { | |
const json = JSON.parse(fileContents); | |
entries.push("newEntry"); | |
entries.forEach((entry) => { | |
json.entries.push(entry); | |
}); | |
return JSON.stringify(json); | |
} | |
module.exports = { | |
update | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment