Doing multiple lines of code on one line

I think you’re missing the point @DanCouper and @kevinSmith were trying to make, it’s just wrong. Yes we can do it, but no one agrees we should. You are most certainly allowed to fit as much as you want on a single line, it is what the semi colon symbol is for, to indicate the end of a single statement, allowing you to group statements on a single line. This is why it is a good idea to always include semicolons even though JS allows for mistakes or outright excluding them.

As far as stretching the ternary condition over multiple lines, even this is not what it was meant for, and still requires that you extensively extend the nesting of your code horizontally leaving ugly white space breaks.

Here is an example of how it works best.

let president = russianInfluence ? "Trump" : "Hillary";

@John-freeCodeCamp btw I was inspired by this discussion and added a volume function to my library. Same structure as the area() above, feel free to use this if you’d like.

/*
	volume("cube", 2) -> 8
	volume("pyramid", 2, 8, 4) -> 21.33...
        User may decide to solve for base (B) themselves
	volume("pyramid", (2 * 4), 8) -> 21.33...
*/
function volume( type = "", dimensions ){
  const w = arguments[1];
  const h = arguments[2];
  const l = arguments[3];
  const volumes = {
		// SOME FORMULAE USE 'base' OR 'b' AS A PRE-CALCULATED AREA OF w AND l
		// FOR THOSE THAT DO, IF l IS UNSET, DETERMINE THAT BASE WAS CALCULATED FOR BY USER AS w
		// THIS RENDERS THE NEED FOR A SEPARATE 'rectangular prism' FROM prism() UNNECESSARY
		cone( r, h ){ return ( Math.PI * (r * r) * h ) / 3; },
		cube( i ){ return i * i * i; },
		cylinder( r, h ){ return ( Math.PI * (r * r) ) * h; },
		hemisphere( r ){ return ( 2 * Math.PI * (r * r * r) ) / 3; },
		prism( w, h, l ){ return l !== undefined ? (w * l) * h : w * h; },
		/* rectanglePrism( w, h, l ){ return w * h * l; }, */
		pyramid( w, h, l ){ return l !== undefined ? ( (w * l) * h ) / 3 : (w * h) / 3; },
		radius( w, h ){ return Math.sqrt( w / (Math.PI * h) ); },
		rectangle( w, h, l ){ return w * h * l; },
		sphere( r ){ return ( 4 * Math.PI * (r * r * r) ) / 3; }
  }

  return volumes[type.toLowerCase()]( w, h, l );
}

@Emgo-Dev

Yet the Trump Russian investigation brought up nothing in how many months? Yet Hillary leaking classified information on a unprotected server to Russia and china, along with deleting 33,000 emails means nothing?

(you spelled Hillary wrong)


Anyways i like your code. So basically your returning the result of a function, and i like how you put it on each line, saving space.

I never would’ve thought of that. I’ll use that idea when i do something like this again, thanks :smiley:

Just wanted to make it funny and applicable haha.

Let’s keep politics out of it, please.

1 Like

Yeah lol lets not___

Ok, I dragged myself here and finished making my account to explain this much much better.

In computer science there is a mathematical technique for measuring the time it will take a method or algorithm. It’s called O() notation.

Here:
https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

1 Like

Wow thanks, so it will take too long to run?

While you here, try freeCodeCamp, learn some basic front end development skills.

Yeah, big O notation is an important topic, but I’m not sure how it is relevant to this specific topic. The issue here wasn’t performance but readability and best practices. I’m not sure what point he was trying to make.

It looked like advertising to me.

1 Like

Yeah, I was suspicious of that too - first time post, only post, not quite relevant. It’s just an odd thing to do stealth advertising for. Especially when he could have easily opened a discussion about big O notation.