Hi there, dear community!
I’ve decided to come to a halt as I was trying to finish the last step of the Mini Linter Project Project (since I was kind of overwhelmed by it) to trace back my steps to the very beginning of the course and do some random practice in the way.
I then came back to the Temperature Converter (Kelvin Weather) Project from the introduction unit and I started playing around with it to see if I’d properly grasps some of the concepts one sees in the following units.
This was my first attempt:
// Temperature Converter 1
const todaysTemperatureInKelvin = 293;
console.log(`Today is ${todaysTemperatureInKelvin} degrees Kelvin.`); // Returns "Today is 293 degrees Kelvin."
function convertToCelsius (todaysTemperatureInKelvin) {
return todaysTemperatureInKelvin - 273;
}
console.log(`Today is ${convertToCelsius(todaysTemperatureInKelvin)} degrees Celsius.`); // Returns "Today is 20 degrees Celsius."
let todaysTemperatureInCelsius = convertToCelsius(todaysTemperatureInKelvin);
function convertToFahrenheit (todaysTemperatureInCelsius) {
return todaysTemperatureInCelsius * (9/5) + 32;
}
let todaysTemperatureInFahrenheit = convertToFahrenheit(todaysTemperatureInCelsius);
todaysTemperatureInFahrenheit = Math.floor(todaysTemperatureInFahrenheit);
console.log(`Today is ${todaysTemperatureInFahrenheit} degrees Fahrenheit.`); // Returns "Today is 68 degrees Fahrenheit."
Out of it, I achieved three proper results and I was kinda happy with them. These results were:
- Today is 293 degrees Kelvin.
- Today is 20 degrees Celsius.
- Today is 68 degrees Fahrenheit.
(Yep, in this case I forgot about Newton degrees.)
Thus, my second attempt (introducing Newton degrees and arrow functions):
// Temperature converter 2
const KelvinTemp = 293;
console.log(`Today is ${KelvinTemp} degrees Kelvin.`); // Returns «Today is 293 degrees Kelvin.»
let CelsiusTemp = (KelvinTemp) => {
return KelvinTemp - 273;
}
console.log(`Today is ${CelsiusTemp(KelvinTemp)} degrees Celsisus.`); // Returns "Today is 20 degrees Celsius."
let FahrenheitTemp = (CelsiusTemp) => {
return Math.floor(CelsiusTemp * (9/5) + 32);
}
console.log(`Today is ${FahrenheitTemp(CelsiusTemp(KelvinTemp))} degrees Fahrenheit.`); // Returns "Today is 68 degrees Fahrenheit."
let NewtonTemp = (CelsiusTemp) => {
return Math.floor(CelsiusTemp * (33/100));
}
console.log(`Today is ${NewtonTemp(CelsiusTemp(KelvinTemp))} degrees Newton.`); // Returns "Today is 6 Newton."
So far, so good. Then, my third attempt: to compile it all in just one function, such as:
const kelvin = 293
function tempConverter (kelvin) {
let celsius = kelvin - 273;
let fahrenheit = Math.floor(celsius * (9/5) + 32);
let newton = Math.floor(celsius * (33/100));
return `${kelvin} degrees Kelvin is ${celsius} degrees Celsius, ${fahrenheit} degrees Fahrenheit and ${newton} degress Newton.`;
}
console.log(tempConverter(293));
And the thing also worked. Great!
Then, I tried mixing a few more concepts… and it was when apocalypse kinda overcame. At first, I was happy with this:
let temperature;
let degrees = ['Kelvin', 'Celsius', 'Fahrenheit', 'Newton'];
let kelvin;
let celsius;
let fahrenheit;
let newton
let tempConverter = (temperature, degrees) => {
switch (degrees) {
case 'Kelvin':
kelvin = temperature;
celsius = kelvin - 273;
fahrenheit = Math.floor(celsius * (9/5) + 32);
newton = Math.floor(celsius * (33/100));
return `${kelvin} degrees Kelvin is ${celsius} degrees Celsius, ${fahrenheit} degrees Fahrenheit and ${newton} degrees Newton.`;
break;
case 'Celsius':
celsius = temperature;
kelvin = celsius + 273;
fahrenheit = Math.floor(celsius * (9/5) + 32);
newton = Math.floor(celsius * (33/100));
return `${celsius} degrees Celsius is ${kelvin} degrees Kelvin, ${fahrenheit} degrees Fahrenheit and ${newton} degrees Newton.`;
break;
case 'Fahrenheit':
fahrenheit = temperature;
celsius = Math.floor((fahrenheit - 32) * 5/9);
kelvin = celsius + 273;
newton = Math.floor(celsius * (33/100));
return `${fahrenheit} degrees Fahrenheit is ${celsius} degrees Celsius, ${kelvin} degrees Kelvin and ${newton} degrees Newton.`;
break;
case 'Newton':
newton = temperature;
celsius = Math.floor(newton / 0.3);
kelvin = celsius + 273;
fahrenheit = Math.floor(celsius * (9/5) + 32);
return `${newton} degrees Newton is ${celsius} degrees Celsius, ${kelvin} degrees Kelvin and ${fahrenheit} degrees Fahrenheit.`;
default:
return 'Choose the proper degrees.';
}
}
As I logged the following, it all printed proper results:
console.log(tempConverter(293,'Kelvin')); // Returns "293 degrees Kelvin is 20 degrees Celsius, 68 degrees Fahrenheit and 6 degrees Newton."
console.log(tempConverter(20,'Celsius')); // Returns "20 degrees Celsius is 293 degrees Kelvin, 68 degrees Fahrenheit and 6 degrees Newton."
console.log(tempConverter(68,'Fahrenheit')); // Returns "68 degrees Fahrenheit is 20 degrees Celsius, 293 degrees Kelvin and 6 degrees Newton."
console.log(tempConverter(6,'Newton')); // Returns "6 degrees Newton is 20 degrees Celsius, 293 degrees Kelvin and 68 degrees Fahrenheit."
But I realized that
console.log(tempConverter('a','Newton'));
console.log(tempConverter(22, 'Ohyeah'));
console.log(tempConverter('Ohyeah', 'Ohyeah'));
returned inappropriate results. Respectively:
-
a degrees Newton is NaN degrees Celsius, NaN degrees Kelvin and NaN degress Fahrenheit.
— at console.log(tempConverter('a', 'Newton'));
-
Choose the proper degrees.
— atconsole.log(tempConverter(22, 'Ohyeah'));
-
Choose the proper degrees.
— atconsole.log(tempConverter('Ohyeah', 'Ohyeah'));
And so, I decided that I wanted to go ahead and try some validation processes. Which is why i wrote:
let temperature;
let degrees = ['Kelvin', 'Celsius', 'Fahrenheit', 'Newton'];
let kelvin;
let celsius;
let fahrenheit;
let newton
let tempConverter = (temperature, degrees) => {
if (typeof temperature != 'number') {
if (degrees != 'Kelvin' || degrees != 'Celsius' || degrees != 'Fahrenheit' || degrees != 'Newton') {
return 'Temperature is not a number and you should choose a proper name for degrees.'
} else if (typeof temperature === 'number') {
switch (degrees) {
case 'Kelvin':
kelvin = temperature;
celsius = kelvin - 273;
fahrenheit = Math.floor(celsius * (9/5) + 32);
newton = Math.floor(celsius * (33/100));
return `${kelvin} degrees Kelvin is ${celsius} degrees Celsius, ${fahrenheit} degrees Fahrenheit and ${newton} degress Newton.`;
break;
case 'Celsius':
celsius = temperature;
kelvin = celsius + 273;
fahrenheit = Math.floor(celsius * (9/5) + 32);
newton = Math.floor(celsius * (33/100));
return `${celsius} degrees Celsius is ${kelvin} degrees Kelvin, ${fahrenheit} degrees Fahrenheit and ${newton} degress Newton.`;
break;
case 'Fahrenheit':
fahrenheit = temperature;
celsius = Math.floor((fahrenheit - 32) * 5/9);
kelvin = celsius + 273;
newton = Math.floor(celsius * (33/100));
return `${fahrenheit} degrees Fahrenheit is ${celsius} degrees Celsius, ${kelvin} degrees Kelvin and ${newton} degress Newton.`;
break;
case 'Newton':
newton = temperature;
celsius = Math.floor(newton / 0.3);
kelvin = celsius + 273;
fahrenheit = Math.floor(celsius * (9/5) + 32);
return `${newton} degrees Newton is ${celsius} degrees Celsius, ${kelvin} degrees Kelvin and ${fahrenheit} degress Fahrenheit.`;
default:
return 'Choose the proper degrees.';
}
}
}
}
But the results were so discouraging:
-
undefined
atconsole.log(tempConverter(293, 'Kelvin'));
-
undefined
atconsole.log(tempConverter(20, 'Celsius'));
-
undefined
at console.log(tempConverter(68, 'Fahrenheit'));
-
undefined
at console.log(tempConverter(6, 'Newton'));
-
Temperature is not a number and you should choose a proper name for degrees.
— at console.log(tempConverter('a', 'Newton'))
; -
undefined
atconsole.log(tempConverter(22, 'Ohyeah'));
-
Temperature is not a number and you should choose a proper name for degrees.
— at console.log(tempConverter('Ohyeah', 'Ohyeah'));
24 hours later, if I try to go over the code or to even write the validation process all over again, I end up writing the very same things, so something is quite missing in my reasoning process, but… what exactly?
Anyways, thanks for those who got to read this far and even more thanks to those of you guys who can help me out of this one
Cheers!