Hey guys I'm currently working on a JSDares challenge that I just can't seem to wrap my head around.
Here's the link, it's called "More Information":
https://jsdares.com/?dare=300000000000000000000109
Here is the code I currently have:
function person(name, born, died, knownFor) {
if(died == 0) {
console.log("Name : " + name);
console.log("Born in : " + born);
console.log("Known for : " + knownFor);
console.log("");
} else {
console.log("Name : " + name);
console.log("Born in : " + born);
console.log("Died in : " + died);
console.log("Known for : " + knownFor);
console.log("");
}
}
console.log("Famous people in computing:");
console.log("");
person("Charles Babbage", 1815, 1871, "first computer");
person("Ada Lovelace", 1815, 1852, "first programmer");
person("George Boole", 1815, 1864, "Boolean logic");
person("Grace Hopper", 1906, 1992, "first language");
person("Alan Turing", 1912, 1954, "Turing machine");
person("Douglas Engelbart", 1925, 0, "Computer mouse");
person("Bill Gates", 1955, 0, "Microsoft");
person("Steve Jobs", 1955, 2011, "Apple");
person("Linus Torvalds", 1969, 0, "Linux");
person("Tim Berners-Lee", 1955, 0, "World Wide Web");
console.log("And many more...");
What I can't seem to figure out is how to reduce the amount of lines I'm using. When I use an IF statement, inside the function, I end up writing a CONSOLE.LOG for every PARAMETER and I can't seem to find an operator or method that will exclude the "DIED" parameter in the ELSE part of the statement. Any tips?
You don't need to use your if
statement for the entire function, but only for one line. I've fix your code in the example below, and also tested it on your submission website:
function person(name, born, died, knownFor) {
console.log("Name : " + name);
console.log("Born in : " + born);
if (died != 0) {
console.log("Died in : " + died);
}
console.log("Known for : " + knownFor);
console.log("");
}
console.log("Famous people in computing:");
console.log("");
person("Charles Babbage", 1815, 1871, "first computer");
person("Ada Lovelace", 1815, 1852, "first programmer");
person("George Boole", 1815, 1864, "Boolean logic");
person("Grace Hopper", 1906, 1992, "first language");
person("Alan Turing", 1912, 1954, "Turing machine");
person("Douglas Engelbart", 1925, 0, "Computer mouse");
person("Bill Gates", 1955, 0, "Microsoft");
person("Steve Jobs", 1955, 2011, "Apple");
person("Linus Torvalds", 1969, 0, "Linux");
person("Tim Berners-Lee", 1955, 0, "World Wide Web");
console.log("And many more...");