Angelica Inguanzo
Created January 31, 2015

Temperature Converter

The Temperature Converter is an app that allows the user to convert from Fahrenheit, Celsius and Kelvin to the metric of their choice.   

Full instructions provided46
Temperature Converter

Code

file_10725.txt

Plain text
General/Overal Description:
Description:

The Temperature Converter is an app that allows the user to convert
from Fahrenheit, Celsius and Kelvin to the metric of their choice.  
The app is separated into 2 sections, the top section is the users
input where they select an initial temperature value and metric.  In
the bottom section the user selects the metric which they would like
to see their input converted and the app outputs the correct value. 

file_10694.js

JavaScript
Final Code for Submission
var THEME = require('themes/flat/theme');
var SLIDERS = require('controls/sliders');
var tempVal = "";
var convertedVal = "";
var BUTTONS = require('controls/buttons');
var startMeasurementVal = "";
var endMeasurementVal = "";
var started = "False"
var lastSelected = ""


//skins and styles
var whiteS = new Skin({fill:"white"});
var lightBlueS = new Skin({fill:"#89CFF0"});
var labelStyle = new Style( { font: "bold 40px", color:"#E52B50" } );

var labelFromStyle = new Style( { font: "bold 40px", color:"gray" } );
var labelToStyle = new Style( { font: "bold 40px", color:"#3B444B" } );

var smallLabelStyle = new Style( { font: "bold 20px", color:"#E52B50" } );

var bigText = new Style({font:"bold 20px", color:"#333333"});

//constant labels
var myFromLabel = new Label({left:0, right:250, height: 20, top: 10, string: "From:", style: smallLabelStyle})
var myToLabel = new Label({left:0, right:250, height: 20, bottom: 160, string: "To:", style: smallLabelStyle})


//slider info
var myLabel = new Label({left:0, right:0, height: 20, top: 30, string: "start", style: labelStyle})
var myConvertedLabel = new Label({left:0, right:0, height: 20, bottom: 130, string: "", style: labelStyle})

var mySlider = SLIDERS.HorizontalSlider.template(function($){ return{
	height:50, left:50, right:50,
	behavior: Object.create(SLIDERS.HorizontalSliderBehavior.prototype, {
		onValueChanged: { value: function(container){
			SLIDERS.HorizontalSliderBehavior.prototype.onValueChanged.call(this, container);
			if(started == "False"){
				myLabel.style = smallLabelStyle;
				myLabel.string = "Select Value";
			} else {
				myLabel.style = labelStyle;
				myLabel.string = tempVal;		
			}
			started = "True";
			tempVal = Math.round(this.data.value);
			
			fromOneToOther();
					
	}}})
}});


//conversion Function
function convertFunction(measure) {
	convertedVal = tempVal;
	myConvertedLabel.string = measure;
};


///from Fahrenheit,Celsius,Kelvin to all
function fromOneToOther(){
			if (startMeasurementVal == "Fahrenheit") {
				if (endMeasurementVal == "Fahrenheit") {
					convertedVal = Math.round(tempVal);
					convertFunction(convertedVal);
				}
				else if (endMeasurementVal == "Celsius") {
					convertedVal = Math.round((tempVal - 32)/1.8);
					convertFunction(convertedVal);
				}
				else if (endMeasurementVal == "Kelvin") {
					convertedVal = Math.round((((tempVal - 32) * 5)/9) + 273.15);
					convertFunction(convertedVal);
				}
			}		
			
			//from Celsius to all
			if (startMeasurementVal == "Celsius") {
				if (endMeasurementVal == "Fahrenheit") {
					convertedVal = Math.round((tempVal*1.8)+32);
					convertFunction(convertedVal);
				}
				else if (endMeasurementVal == "Celsius") {
					convertedVal = Math.round(tempVal);
					convertFunction(convertedVal);
				}
				else if (endMeasurementVal == "Kelvin") {
					convertedVal = Math.round(tempVal + 273.15);
					convertFunction(convertedVal);
				}
			}
			
			//from Kelvin to all
			if (startMeasurementVal == "Kelvin") {
				if (endMeasurementVal == "Fahrenheit") {
					convertedVal = Math.round(((tempVal - 273.15) * 1.8) + 32);
					convertFunction(convertedVal);
				}
				else if (endMeasurementVal == "Celsius") {
					convertedVal = Math.round(tempVal - 273.15);
					convertFunction(convertedVal);
				}
				else if (endMeasurementVal == "Kelvin") {
					convertedVal = Math.round(tempVal);
					convertFunction(convertedVal);
				}
			}
};


//measurement
var myStartMeasurement = BUTTONS.RadioGroup.template(function($){ return{
	top:60, left:50, right:50,
	behavior: Object.create(BUTTONS.RadioGroupBehavior.prototype, {
		onRadioButtonSelected: { value: function(buttonName){
			startMeasurementVal = buttonName;
			if(lastSelected == "bottom") {
				myLabel.style = smallLabelStyle;
				myLabel.string = "Select Value";
				myConvertedLabel.string = "";
				lastSelected = "top";
			}
	}}})
}});


var myEndMeasurement = BUTTONS.RadioGroup.template(function($){ return{
	bottom:30, left:50, right:50,
	behavior: Object.create(BUTTONS.RadioGroupBehavior.prototype, {
		onRadioButtonSelected: { value: function(buttonName){
			endMeasurementVal = buttonName;
			if(tempVal != "" && startMeasurementVal !=  "" && endMeasurementVal != ""){
			fromOneToOther();	
			lastSelected = "bottom"
			if(myLabel.string == "Select Value") {
				myConvertedLabel.string = "";
			}			
		}
	}}})
}});


// calls
var slider = new mySlider({ min:-50, max:115, value:33,  });
var startMeasurement = new myStartMeasurement({ buttonNames: "Fahrenheit,Celsius,Kelvin" });
var endMeasurement = new myEndMeasurement({ buttonNames: "Fahrenheit,Celsius,Kelvin" });


//main
var redS = new Skin({fill:"#F4C2C2"});
var blueS = new Skin({fill:"#D19FE8"});
var whiteSS = new Skin({
	fill:"89CFF0", 
	borders:{left:0, right:0, top:15, bottom:15}, 
	stroke:"gray"
});
var mainCon = new Container({left:0, right:0, top:0, bottom:0, skin: lightBlueS});
var col = new Column({
	left:0, right:0, top:0, bottom:0,
	contents:[
		new Line({left:0, right:0, top:0, bottom:0,height: 80, skin: redS}),
		new Line({left:0, right:0, top:0, bottom:0, skin: whiteSS,
			contents:[
				new Content({left:0, right:0, top:0, bottom:0, skin: whiteSS}),
			]
		}),
		new Line({left:0, right:0, top:0, bottom:0, height: 80, skin: blueS})
	]
});
	 
application.add(mainCon);
mainCon.add(col);
mainCon.add(myLabel);
mainCon.add(myFromLabel);
mainCon.add(myToLabel);
mainCon.add(myConvertedLabel);
mainCon.add(startMeasurement);
mainCon.add(endMeasurement);
mainCon.add(slider);

hw1.zip

Plain text
Zip File
Error opening file.

Github

main.js saved in src folder. App opens in Kinoma directly if download/unzip then select File > OpenFile > cs160-master> .project. Otherwise the main.js code is also provided above.

Credits

Angelica Inguanzo
4 projects • 0 followers

Comments