Browse Source

Finish Functions Basics

Signed-off-by: Michael Tang <michael.h.tang@gmail.com>
Michael Tang 5 năm trước cách đây
commit
321ee489ce
1 tập tin đã thay đổi với 34 bổ sung0 xóa
  1. 34 0
      functions-101.js

+ 34 - 0
functions-101.js

@@ -0,0 +1,34 @@
+// Function - input (argument), code, output (return value)
+
+let greetUser = function () {
+    console.log('Welcome user!')
+}
+
+greetUser()
+
+let square = function (num) {
+    let result = num * num
+    return result
+}
+
+let value = square(3)
+let otherValue = square(10)
+
+console.log(value)
+console.log(otherValue)
+
+// Challenge Area
+
+// convertFahrenheitToCelsius
+let convertFahrenheitToCelsius = function(fahrenheit) {
+    let celsius = (fahrenheit - 32) * (5/9)
+    return celsius
+}
+
+// Call a couple of times (32 -> 0) (68 -> 20)
+let convertedTemp = convertFahrenheitToCelsius(32)
+let otherConvertedTemp = convertFahrenheitToCelsius(68)
+
+ // Print the converted values
+ console.log(convertedTemp)
+ console.log(otherConvertedTemp)