Tell us what’s happening:
runs and show results as expected but don`t pass the test. I think it expects a solution without if. can you suggest anything? My key is MESSAGE_STYLE and its value is uppercase.
var express = require(‘express’);
var app = express();
absolutePath = __dirname + “/views/index.html”
assetsAbsolutePath = __dirname + “/public/style.css”
When MESSAGE_STYLE have a value it is true, even when I pass process.env.MESSAGE_STYLE == “uppercase” it says it is the wrong answer app.get("/json", (req, res) => { let message = "Hello json"; (process.env.MESSAGE_STYLE == "uppercase") ? message=message.toUpperCase() : message=message; res.json({"message": message}); });
The test in the background sets process.env.MESSAGE_STYLE to undefined however the environment variable does not contain the primitive values undefined it holds the string 'undefined'. Which is a truthy value.
‘it expects an obj’? I’m not sure what you are talking about.
You have written you code in a way where it is hard to see what you are doing
app.get("/json",
(req, res) => {
// Set default value of the message
let message = "Hello json";
// ternary, but WHY?
// This isn't how you use a ternary
(process.env.MESSAGE_STYLE == "uppercase") ? message=message.toUpperCase() : message=message;
// Respond with message
res.json({"message": message});
}
);
A ternary has a return value. Any time you are throwing away a return value, you should be suspicious of what you are doing.
If you think the way I wrote my code is hard to read is because I am learning to program so I dont have the proper skill to write clean code, besides that, I was struggling to solve this challenge. *"Then, in the /json GET route handler you created in the last challenge, transform the response object's message to uppercase if process.env.MESSAGE_STYLE equals uppercase"* Thats why I am thinking I have to transform process.env.MESSAGE_STYLE to a object.
app.get("/json",
(req, res) => {
// THESE NAMES ARE A BIT CONFUSING
const message = process.env.MESSAGE_STYLE;
let json = "Hello json";
// WHY NOT REPLACE THIS WITH A REAL IF?!?
// YOU SHOULD NOT USE A TERNARY IF YOU
// ARE IGNORING THE RETURN VALUE
// GOOD:
// let myResult = cond ? resultTrue : resultFalse;
// BAD:
// cond ? actionTrue : actionFalse;
// BETTER:
// if (cond) {
// actionTrue;
// } else {
// actionFalse;
// }
(process.env.MESSAGE_STYLE == "uppercase") ? json=json.toUpperCase() : json=json;
res.json({"message": json});
// WHY WOULD YOU EXPECT AN OBJECT HERE?
console.log(typeof process.env.MESSAGE_STYLE);
}
);
What is the current result from the tests for this code?