Here's a complete list of the issues you have in your code:
- Using
or
instead of ||
(as others have stated)
- Using incorrect variable name in the
convertToFarh
function
- Omitting
type
parameter when calling convertToFarh
- Attempting to call
String.toLowerCase()
on a number
variable
- Misspelling
fahrenheit
Using or
instead of ||
(Logical OR)
In your code, I believe you are intending to indicate if (val > 50 || val == 50) {
instead of using or
as used in other programming languages.
Before:
if (val > 50 or val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 or val == 50){
print("it is hot, you should wear a tank top")
}
After:
if (val > 50 || val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 || val == 50){
print("it is hot, you should wear a tank top")
}
This logic also doesn't make sense. Perhaps you mean value < 50
in the first one and val >= 50
in the second?
You also repeat yourself whether you're converting to/from Kelvin or Celsius, so that code could be extracted out into its own function or just reduce the if..else
blocks down to only affect the necessary variables and perform the comparison after these blocks.
Using incorrect variable name in convertToFarh
function
In the convertToFarh
function, you reference a variable named far
, but there's no variable by that name. So you either mean to reference the val
argument or you are trying to reference the fahr
variable declared outside the function. In my code, I assume the former is the case val
and rename it as follows:
function convertToFarh(type, val){
type.toLowerCase()
val.toLowerCase()
if (type == "kelvin"){
return Math.floor(val / (9/5) - 32 - 273)
}
else if(type == "celsius"){
return Math.floor(val / (9/5) - 32)
}
}
Omitting type
parameter when calling convertToFarh
In both function calls to convertToFarh
, you use the c
or k
variable as the value of the val
parameter, but you don't indicate the type. I have fixed this to indicate the type for each part:
var val = convertToFarh("celsius", c);
var val = convertToFarh("kelvin", k);
Attempting to call String.toLowerCase()
on a number
variable
In the convertToFarh
function, you are attempting to call the String.toLowerCase()
method on a number
type (val
) which gives an error. In my code, I simply commented this out and confirmed it can safely be removed.
Misspelling fahrenheit
It might not seem like a big deal, but making sure variables have proper spelling helps when others are reviewing your code (whether bug-fixing or general code review). I have fixed function names, variable names, and any references to fahrenheit
in your code to be the proper spelling. This includes:
"enter a fahrenheit temperature: "
function fahrToKelvin
and function calls
function fahrToCelsius
and function calls
function convertToFahr
and function calls
- The variable named
farh
to fahr
- Function parameters named
far
were changed to val
to avoid variable name collision
Full code
function print(str) {
console.log(str);
}
function fahrToKelvin(val) {
const cel = (val - 32) / (9 / 5);
return Math.floor(cel + 273.15);
}
function fahrToCelsius(val) {
const cel = Math.floor((val - 32) * 5 / 9);
return cel;
}
function convertToFahr(type, val) {
if (type == "kelvin") {
return Math.floor(val / (5 / 9) - 459.67);
} else if (type == "celsius") {
return Math.floor(val / (9 / 5) + 32);
}
}
var fahr = prompt("enter a fahrenheit tempature: ");
var type = prompt("convert it to celsius, or kelvin");
type = type.toLowerCase();
if (type == "celsius") {
const c = fahrToCelsius(fahr);
var val = convertToFahr("celsius", c);
if (val < 50) {
print("it is cold, you should wear a coat");
}
if (val >= 50) {
print("it is hot, you should wear a tank top");
}
} else if (type == "kelvin") {
const k = fahrToKelvin(fahr);
var val = convertToFahr("kelvin", k);
if (val < 50) {
print("it is cold, you should wear a coat");
}
if (val >= 50) {
print("it is hot, you should wear a tank top");
}
}