Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candy Leong #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
console.log("hello script js");
var inputHappened = function(currentInput){
var str = currentInput;
var lastChar = str[str.length -1];
var temperature = parseInt(str.substring(0,str.length-1), 10);
if (!isNaN (temperature))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
parseInt returns either the integer (truthy value) or a NaN (falsy value)
so you can also refactor this to a shorter condition if (temperature)

{
if (lastChar == "F" || lastChar == "f")
{
return output ="It is "+((temperature-32)*5/9+273.15).toFixed(2)+ "K and "+((temperature)*5/9).toFixed(2)+"°C";
}

else if (lastChar == "K" || lastChar == "k")
{
return output ="It is "+((temperature-273.15) * 9/5 + 32).toFixed(2)+ "F and "+(temperature - 273.15).toFixed(2)+"°C";
}
else if (lastChar == "C" || lastChar == "c")
{
return output ="It is "+(temperature * 9/5) + 32).toFixed(2)+ "F and "+temperature + 273.15.toFixed(2)+"K";
}
else {
return output = "Please input a unit (F,C,K)"
}

}
else {
return output = "Please input a number"
}
}
var inputHappened = function(currentInput){
console.log( currentInput );
var output = "WOW TEMPERATURE";
return output;
};
var str = currentInput;
var lastChar = str[str.length -1];
var temperature = parseInt(str.substring(0,str.length-1), 10);
if (!isNaN (temperature))
{
if (lastChar == "F" || lastChar == "f")
{
return output ="It is "+((temperature-32)*5/9+273.15).toFixed(2)+ "K and "+((temperature)*5/9).toFixed(2)+"°C";
}

else if (lastChar == "K" || lastChar == "k")
{
return output ="It is "+((temperature-273.15) * 9/5 + 32).toFixed(2)+ "F and "+(temperature - 273.15).toFixed(2)+"°C";
}
else if (lastChar == "C" || lastChar == "c")
{
return output ="It is "+(temperature * 9/5) + 32).toFixed(2)+ "F and "+temperature + 273.15.toFixed(2)+"K";
}
else {
return output = "Please input a unit (F,C,K)"
}

}
else {
return output = "Please input a number"
}
}