Singular-plural agreement

Preformatted textHi, I’m HydeB and I’m new here.

I’m having a bit of problem with a few lines of code and I’d like to understand what I’m doing wrong: I’m a newbie to JS.

It is a simple conversion of temperatures. I can get it all done but for a thing: between -1 and 1, I need the console to display ‘degree’ instead of ‘degrees’.

Here’s my code:

let cn, cf;

if (f > 1) {cf = 'degrees'}
	else if (f < 1) {cf = 'degree'}; //works as expected: -9 'degree' and 2 'degrees';

if (n > 1) {cn = 'degrees'}
	else if (n < 1, n < -1) {cn = 'degree'}; //returns 'undefined'.
		//else if (n < -1) {cn = 'degrees'}; //doesn't work;

Messing around with the <> symbols don’t help.

I tried to look up for some kind of interval, something as in ‘else if (n = -1:1)’. No such thing. All I got for intervals was loop instructions.

I’d appreciate any pointers.

This isn’t a valid condition.

Side note - you should really use some sort of consistent formatting. Your code is hard to read.

1 Like
else if (n < 1, n < -1) {cn = ‘degree’}; //returns ‘undefined’.
else if (n < -1) {cn = ‘degrees’}; //doesn’t work;

in the first line, I think you are trying to use logical AND &&. instead of using , which creates two conditions that are invaild.

Logical AND (&&) - JavaScript | MDN (

your issue is that the code is invaild, but is executable. the way it excuted, that it checks if (n < 1) while ignoring (n > -1). so if you fix the first else if statement you will be good to go.

1 Like

Got it:

cn = ‘degrees’;

if (n <= 1) {cn = ‘degree’};
if (n <= -1) {cn = ‘degrees’};

Thanks for the tip and for taking the time to help me.

Thanks for the tip on && and for taking the time to help me.

1 Like

Did as you suggested and works too:

let k, kc;

k = 1

kc = 'degrees'

if (k <= 1  &&  k >= -1)  {kc = 'degree'};

console.log(`The temperature is ${k} ${kc}.`);

PS: Is my code a bit clearer?

Tks again!

is my code a bit clearer

I won’t lie, not really.

I think what jeremy meant with format is adding defining names that easy to understand instead of k ks for example.

let temperature, degree;

temperature = 1;

degree = "fahrenheits"

if (temperature <= 1  &&  temperature >= -1) {
degree = 'fahrenheit'

console.log(`The temperature is ${temperature} ${degree}.`);

don’t worry about spacing and stuff like that, most IDE take care of it, and there is prettier to back you up if needed.

Prettier · Opinionated Code Formatter

1 Like

I was thinking a bit about variable names, but also about indentation.

A formatter is good and will help, but some of your initial formatting choices made your code harder to read.

Inconsistent indentation is harder to read. Also, it’s a bit strange to use {} when you stick the if statement body all on the same line as the condition.

if (condition) { /* this is odd */ }
  else if (condition2) { /* also this indentation is odd */ }

if (condition) {
  // this is more standard
} else {
  // and consistent 

if (condition1) /* or this */;
else if (condition2) /* with this */;

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.