Discussion:
JavaScript Fundamentals - Building A Basic Calculator With JavaScript - Part 2
(too old to reply)
Apd
2025-01-23 20:56:29 UTC
Permalink
if (operator === '+') {
return a + b;
} else if (operator === '-') {
return a - b;
} else if (operator === '*') {
return a * b;
} else if (operator === '/') {
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
} else {
return 'Invalid operator! Use only +, -, *, or /.';
}
}
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause. You can omit the "break" between each
one because of the immediate return:

switch (operator) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
default:
return 'Invalid operator! Use only +, -, *, or /.';
}
Steve Carroll
2025-01-23 21:06:56 UTC
Permalink
Post by Apd
if (operator === '+') {
return a + b;
} else if (operator === '-') {
return a - b;
} else if (operator === '*') {
return a * b;
} else if (operator === '/') {
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
} else {
return 'Invalid operator! Use only +, -, *, or /.';
}
}
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause. You can omit the "break" between each
switch (operator) {
return a + b;
return a - b;
return a * b;
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
return 'Invalid operator! Use only +, -, *, or /.';
}
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)

FWIW, this needn't be limited to what I write, if you want to write a
more complete Ref page #2 (with things I left out, i.e. "BigInt" in the
data types) I'd welcome it! But I'm not just creating these for this ng
so I have more incentive than you do, given the lack of interest.
Apd
2025-01-23 22:04:32 UTC
Permalink
Post by Steve Carroll
Post by Apd
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause.
[...]
Post by Steve Carroll
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)
All understood. I'm only giving my two-penny worth.
Post by Steve Carroll
FWIW, this needn't be limited to what I write, if you want to write a
more complete Ref page #2 (with things I left out, i.e. "BigInt" in the
data types) I'd welcome it!
Maybe, but I'm ok just adding to these threads. "BigInt", although
its meaning is clear to me, is another feature I haven't yet
encountered.
Post by Steve Carroll
But I'm not just creating these for this ng so I have more incentive
than you do, given the lack of interest.
Whatever, it's good you're posting them.
Brock McNuggets
2025-01-23 22:07:19 UTC
Permalink
Post by Apd
Post by Steve Carroll
Post by Apd
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause.
[...]
Post by Steve Carroll
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)
All understood. I'm only giving my two-penny worth.
For 18 cents more you can change my paradigm.
--
Specialist in unnecessary details and overcomplicated solutions.
Steve Carroll
2025-01-23 22:25:33 UTC
Permalink
Post by Apd
Post by Steve Carroll
Post by Apd
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause.
[...]
Post by Steve Carroll
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)
All understood. I'm only giving my two-penny worth.
Post by Steve Carroll
FWIW, this needn't be limited to what I write, if you want to write a
more complete Ref page #2 (with things I left out, i.e. "BigInt" in the
data types) I'd welcome it!
Maybe, but I'm ok just adding to these threads. "BigInt", although
its meaning is clear to me, is another feature I haven't yet
encountered.
Post by Steve Carroll
But I'm not just creating these for this ng so I have more incentive
than you do, given the lack of interest.
Whatever, it's good you're posting them.
I think so... but I'm not sure why ;)

I didn't cover the regex stuff as you suggested, I did...

"We need to use JS' 'split' method and I'm going to apply a bit of
'regex' that I won't be explaining at this point (maybe elsewhere,
later)."

I'm trying to stay around 100 lines or so. I'll be using this:

<https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle>

But my usage will just be some confusing comments ;)

function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {

// generate a random index between 0 and i (i is current index in loop)
const j = Math.floor(Math.random() * (i + 1));

// shuffle indices i and j (j is a random index used for shuffling)
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}

Maybe I'll include the link <shrug>.

Trying to stay 'condensed' and actually 'get somewhere'... I can't cover
all the details. In any event, this will probably be the last project.
Between the refs, the calc and this text generator I've provided enough
basic info to do other simple projects.
FromTheRafters
2025-01-24 12:46:45 UTC
Permalink
Post by Steve Carroll
Post by Apd
if (operator === '+') {
return a + b;
} else if (operator === '-') {
return a - b;
} else if (operator === '*') {
return a * b;
} else if (operator === '/') {
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
} else {
return 'Invalid operator! Use only +, -, *, or /.';
}
}
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause. You can omit the "break" between each
switch (operator) {
return a + b;
return a - b;
return a * b;
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
return 'Invalid operator! Use only +, -, *, or /.';
}
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)
I was thinking that this is just the sort of thing ME was talking about
earlier. The structure above is very much like BASIC's case select.
Post by Steve Carroll
FWIW, this needn't be limited to what I write, if you want to write a
more complete Ref page #2 (with things I left out, i.e. "BigInt" in the
data types) I'd welcome it! But I'm not just creating these for this ng
so I have more incentive than you do, given the lack of interest.
I'm reading, and I might just respond some so that you don't whack me
in the head with a ruler. :)
Steve Carroll
2025-01-24 14:38:27 UTC
Permalink
Post by FromTheRafters
Post by Steve Carroll
Post by Apd
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause. You can omit the "break" between each
switch (operator) {
return a + b;
return a - b;
return a * b;
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
return 'Invalid operator! Use only +, -, *, or /.';
}
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)
I was thinking that this is just the sort of thing ME was talking about
earlier.
ME was talking about code? Do tell! ;)
Post by FromTheRafters
The structure above is very much like BASIC's case select.
That's the thing about JS, a lot of 'the basics' are similar enough to
some of other stuff that you'll probably pick them (the basics) up
fairly quickly. The 'agony' often comes in 'method use' (and a big part
of that is 'remembering') but once you look at it from the viewpoint
that their existence means, 'YOU didn't have to write them!', all you
have to do is learn how to use them, it's not so painful. As to the
'remembering', the browser helps you out with a form of 'completion' via
the dropdown menu I've mentioned elsewhere.
Post by FromTheRafters
Post by Steve Carroll
FWIW, this needn't be limited to what I write, if you want to write a
more complete Ref page #2 (with things I left out, i.e. "BigInt" in the
data types) I'd welcome it! But I'm not just creating these for this ng
so I have more incentive than you do, given the lack of interest.
I'm reading, and I might just respond some so that you don't whack me
in the head with a ruler. :)
Whachoo talkin' 'bout, Willis?!

This reminded me of Carlin's 'Sister Mary Discipline' line:


FromTheRafters
2025-01-24 15:15:26 UTC
Permalink
Post by Steve Carroll
Post by FromTheRafters
Post by Steve Carroll
Post by Apd
The thing about "switch" is it's designed for this and uses strict
equality on the "case" clause. You can omit the "break" between each
switch (operator) {
return a + b;
return a - b;
return a * b;
if (b === 0) {
return "Can't divide by zero!";
}
return a / b;
return 'Invalid operator! Use only +, -, *, or /.';
}
I'm aware, but I purposely didn't include it in the "incomplete"
Reference doc (there's enough there for a beginner to start crying as
it is). The if/else idea is very 'approachable' so that's why I chose
it... that, and the fact it's more usable for a lot of basic projects.
I'm not writing 'The Complete Guide to JS' here ;)
I was thinking that this is just the sort of thing ME was talking about
earlier.
ME was talking about code? Do tell! ;)
No, he was talking about a high level view as opposed to a more nuts
and bolts view. Many languages have such semantic similarities without
getting into specific syntax.
Post by Steve Carroll
Post by FromTheRafters
The structure above is very much like BASIC's case select.
That's the thing about JS, a lot of 'the basics' are similar enough to
some of other stuff that you'll probably pick them (the basics) up
fairly quickly. The 'agony' often comes in 'method use' (and a big part
of that is 'remembering') but once you look at it from the viewpoint
that their existence means, 'YOU didn't have to write them!', all you
have to do is learn how to use them, it's not so painful. As to the
'remembering', the browser helps you out with a form of 'completion' via
the dropdown menu I've mentioned elsewhere.
Post by FromTheRafters
Post by Steve Carroll
FWIW, this needn't be limited to what I write, if you want to write a
more complete Ref page #2 (with things I left out, i.e. "BigInt" in the
data types) I'd welcome it! But I'm not just creating these for this ng
so I have more incentive than you do, given the lack of interest.
I'm reading, and I might just respond some so that you don't whack me
in the head with a ruler. :)
Whachoo talkin' 'bout, Willis?!
http://youtu.be/HcrkHnelxq0
Yeah, he was funny.

Loading...