This is a bit of an expansion from existing answers, applying some of the suggestions given before.
First, pick a style of quotes. Dont flip-flop between single and double quotes.
Here are 2 examples of it:
prompt("Please enter an egg weight in grams: ");
alert('Peewee');
Due to personal preferences, I will be sticking to single-quotes.
Something that everybody forgot was that everything can be turned into a function:
function getWeight()
{
var eggWeight = prompt('Please enter an egg weight in grams: ');
eggWeight = parseInt(eggWeight);
while(isNaN(eggWeight))
{
eggWeight = prompt('This is not a valid number. Please enter an egg weight in grams: ');
eggWeight = parseInt(eggWeight);
}
return eggWeight;
}
function getEggDesignation(eggWeight)
{
if (eggWeight > 69)
return 'Jumbo';
else if (eggWeight > 63 && eggWeight <= 69)
return 'Extra Large';
else if (eggWeight > 55 && eggWeight <= 63)
return 'Large';
else if (eggWeight > 48 && eggWeight <= 55)
return 'Medium';
else if (eggWeight > 42 && eggWeight <= 48)
return 'Small';
else
return 'Peewee';
}
function calculateEggDesignation()
{
var eggWeight = getWeight();
var designation = getEggDesignation(eggWeight);
alert(designation);
}
This way, if you want to, say, receive input from a known element, you can just change the function related to gathering input.
You make absolutely no efforts at all to see if the prompt()
was cancelled.
You can easily change it to detect if the result is null
:
function getWeight()
{
var eggWeight = prompt('Please enter an egg weight in grams: ');
if(eggWeight === null)
{
return false;
}
eggWeight = parseInt(eggWeight);
while(isNaN(eggWeight))
{
eggWeight = prompt('This is not a valid number. Please enter an egg weight in grams: ');
if(eggWeight === null)
{
return false;
}
eggWeight = parseInt(eggWeight);
}
return eggWeight;
}
function getEggDesignation(eggWeight)
{
[...]
}
function calculateEggDesignation()
{
var eggWeight = getWeight();
if(eggWeight === false)
{
// alert('You cancelled the calculation');
return;
}
var designation = getEggDesignation(eggWeight);
alert(designation);
}
The strict equality (===
) operator is required, due to being possible to accept 0
as an input.
Now, focusing on the getEggDesignation()
function, you can see a non-elegant if
lasagna. Yuck.
You can try to change it to use an object with the minimum weight per "designation":
function getEggDesignation(eggWeight)
{
var eggWeights = {
70: 'Jumbo',
64: 'Extra Large',
56: 'Large',
49: 'Medium',
43: 'Small',
0: 'Peewee'
};
var last_step = 0;
var result = eggWeights[last_step];
for(var k in eggWeights)
{
if(eggWeights.hasOwnProperty(k) && eggWeight >= k && k >= last_step)
{
result = eggWeights[k];
last_step = k;
}
}
return result;
}
Isn't it a beauty? 😍
An alternative could be:
function getEggDesignation(eggWeight)
{
var eggWeights = {
70: 'Jumbo',
64: 'Extra Large',
56: 'Large',
49: 'Medium',
43: 'Small',
0: 'Peewee'
};
var newWeight = Object.keys(eggWeights)
.map(function(weight){ return +weight; })
.sort()
.filter(function(weight){ return eggWeight >= weight; })
.slice(-1);
return eggWeights[newWeight];
}
Basically, it grabs the keys, converts to integers, sorts them (ascending), removes the ones that are higher than eggWeight
and picks the last one.
This new value is then used to get the value from eggWeights
.
Warning: Depending on the execution environment, you may need a polyfill for Object.keys()
, for Array.prototype.map()
and for Array.prototype.filter()
. Possibly Array.prototype.sort()
if you intend to run this on very old browsers.
For ES6:
If your execution environment is recent enough, you can just use arrow functions and const
:
function getEggDesignation(eggWeight)
{
const eggWeights = {
70: 'Jumbo',
64: 'Extra Large',
56: 'Large',
49: 'Medium',
43: 'Small',
0: 'Peewee'
};
const newWeight = Object.keys(eggWeights)
.map(weight => +weight})
.sort()
.filter(weight => eggWeight >= weight})
.slice(-1);
return eggWeights[newWeight];
}
For compatibility sake, I will use the first alternative, despite needing a polyfill for Object.prototype.hasOwnProperty()
for very old browsers.
Another thing I've noticed is that you do not validate the range of inputs at all.
I can say that my egg is a black hole (-10000) and you'll say it is a Peewee.
It's always good to verify if the value is acceptable.
Changing part of the getWeight()
function:
while(isNaN(eggWeight) || eggWeight < 0)
{
eggWeight = prompt('This is not a valid positive number. Please enter an egg weight in grams: ');
[...]
}
The final code:
This is the final implementation, after all the changes:
function getWeight()
{
var eggWeight = prompt('Please enter an egg weight in grams: ');
if(eggWeight === null)
{
return false;
}
eggWeight = parseInt(eggWeight);
while(isNaN(eggWeight) || eggWeight < 0)
{
eggWeight = prompt('This is not a valid positive number. Please enter an egg weight in grams: ');
if(eggWeight === null)
{
return false;
}
eggWeight = parseInt(eggWeight);
}
return eggWeight;
}
function getEggDesignation(eggWeight)
{
var eggWeights = {
70: 'Jumbo',
64: 'Extra Large',
56: 'Large',
49: 'Medium',
43: 'Small',
0: 'Peewee'
};
var last_step = 0;
var result = eggWeights[last_step];
for(var k in eggWeights)
{
if(eggWeights.hasOwnProperty(k) && eggWeight >= k && k >= last_step)
{
result = eggWeights[k];
last_step = k;
}
}
return result;
}
function calculateEggDesignation()
{
var eggWeight = getWeight();
if(eggWeight === false)
{
// alert('You cancelled the calculation');
return;
}
var designation = getEggDesignation(eggWeight);
alert(designation);
}
calculateEggDesignation();
alert()
message box. Also were you explicitely assigned to useprompt()
for this task? Otherwise I agree with @Zeta. \$\endgroup\$var
in Javascript. It breaks all sensible scoping rules. Uselet
orconst
, nevervar
\$\endgroup\$