What is wrong with this?


#1
const kelvin = 294;
const celsius = kelvin - 273;
let fahrenheit = celsius * (9/5) + 32;
fahrenheit = Math.floor(fahrenheit);
console.log(`The temperature is ${fahrenheit} degrees fahrenheit.`);
const kelvin = prompt('What is the kelvin temperature today?');

#2

We could start with this line,

https://en.wikipedia.org/wiki/Absolute_zero

so,

const KELVIN = 0;
const CELSIUS = KELVIN - 273.15;

I bring this up as a less than arbitrary initial value. Knowing the the lowest end of the temperature scale is 0 degrees Kelvin, makes it the best base reference.

const kelvin_to_celsius = kelvin => {
    return kelvin < KELVIN ? "No such temperature" : kelvin + CELSIUS;
};
const celsius_to_kelvin = celsius => {
    return celsius < CELSIUS ? "No such temperature" : celsius - CELSIUS
};

These two scales are directly related, and not proportional. Each scale increases by the same measure, 1 to 1 with the constant offset…

Fahrenheit and Celsius are proportional scales, also with a constant offset, 32.

const celsius_to_fahrenheit = celsius => {
    return celsius < CELSIUS ? "No such temperature" : 9 * celsius / 5 + 32;
};
const fahrenheit_to_celsius = fahrenheit => {
    let temp = 5 * (fahrenheit - 32) / 9;
    return celsius < CELSIUS ? "No such temperature" : temp;
};

Now the last two lines of your code (but in reverse order) make practical sense…

let kelvin = Number(prompt(`What is the Kelvin temperature today?`));
let celsius = kelvin_to_celsius(kelvin);
let fahrenheit = celsius_to_fahrenheit(celsius);

console.log(`The temperature is ${fahrenheit.toFixed(2)} degrees Fahrenheit.`);

Not sure this helps with the lesson since we do not have any way of knowing which one you are on without a link.

https://repl.it/@mtf/Kelvin-Celsius-Fahrenheit-demo

0.00 degrees Kelvin is,
  -273.15 degrees Celsius
  -459.67 degrees Fahrenheit.
-273.00 degrees Celsius is,
  -459.40 degrees Fahrenheit
  0.15 degrees Kelvin.
-459.00 degrees Fahrenheit is,
  -272.78 degrees Celsius
  0.37 degrees Kelvin.
273.00 degrees Kelvin is,
  -0.15 degrees Celsius
  31.73 degrees Fahrenheit.
0.00 degrees Celsius is,
  32.00 degrees Fahrenheit
  273.15 degrees Kelvin.
32.00 degrees Fahrenheit is,
  0.00 degrees Celsius
  273.15 degrees Kelvin.

#3

The above repl is dynamic, so here is a snapshot of where we are now.

const round = Math.round;
const KELVIN = 0;
const CELSIUS = KELVIN - 273.15;

const kelvin_to_celsius = kelvin => {
    return kelvin < KELVIN ? "No such temperature" : kelvin + CELSIUS;
};
const celsius_to_kelvin = celsius => {
    return celsius < CELSIUS ? "No such temperature" : celsius - CELSIUS
};

const celsius_to_fahrenheit = celsius => {
    return celsius < CELSIUS ? "No such temperature" : 9 * celsius / 5 + 32;
};

const fahrenheit_to_celsius = fahrenheit => {
    let celsius = 5 * (fahrenheit - 32) / 9;
    return celsius < CELSIUS ? "No such temperature" : celsius;
};

kelvin = Number(prompt(`What is the Kelvin temperature today?`));
let celsius = kelvin_to_celsius(kelvin)
try {
    let fahrenheit = celsius_to_fahrenheit(celsius);
    console.log(`${kelvin} degrees Kelvin is,
  ${celsius.toFixed(2)} degrees Celsius,
  ${fahrenheit.toFixed(2)} degrees Fahrenheit.`);
}
catch(TypeError) {
    console.log(celsius);
}

celsius = Number(prompt(`What is the Celsius temperature today?`));
kelvin = celsius_to_kelvin(celsius);
let fahrenheit = celsius_to_fahrenheit(celsius);
try {
    console.log(`${celsius} degrees Celsius is,
  ${fahrenheit.toFixed(2)} degrees Fahrenheit,
  ${round(kelvin, 2)} degrees Kelvin.`);
}
catch(TypeError) {
    console.log(fahrenheit);
}

fahrenheit = Number(prompt(`What is the Fahrenheit temperature today?`));
celsius = fahrenheit_to_celsius(fahrenheit);
try {
    let kelvin = celsius_to_kelvin(celsius);
    console.log(`${fahrenheit} degrees Fahrenheit is,
  ${round(kelvin, 2) || kelvin} degrees Kelvin,
  ${celsius.toFixed(2)} degrees Celsius.`);
}
catch(TypeError) {
    console.log(kelvin);
}

As it stands, the text being sent to .toFixed() is the only trip wire. As we can see, celsius gets called up twice and we don’t get to use kelvin in the logic. I’m really tripped up on this point.

Edit 12/1/2017

Solved the logic and have erroneous entries fairly well trapped, as well as code errors fixed. Code sample above updated.


 Native Browser JavaScript
 >
 0 degrees Kelvin is,
   -273.15 degrees Celsius,
   -459.67 degrees Fahrenheit.
 -273.15 degrees Celsius is,
   -459.67 degrees Fahrenheit,
   0 degrees Kelvin.
 -459.67 degrees Fahrenheit is,
   0 degrees Kelvin,
   -273.15 degrees Celsius.
=> undefined
 >

With inputs, -1, -274, and -460 respectively,

 Native Browser JavaScript
 > 
 No such temperature
 No such temperature
 No such temperature
=> undefined
 >

#4

will figure it out thanks


#5

We can help if we know the lesson… Please post a link to the exercise. Thanks.


#6

Extend example showing usage in a web form.

https://repl.it/@mtf/KCF-Equivalencies