Browse Source

homework 2 is partly done

miskson 2 years ago
parent
commit
4aee7d0b0c
2 changed files with 130 additions and 0 deletions
  1. 13 0
      hw2/index1.html
  2. 117 0
      hw2/script1.js

+ 13 - 0
hw2/index1.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>hw2.1</title>
+</head>
+<body>
+    <h1>Homework 2: part1</h1>
+    <script src="./script1.js"></script>
+</body>
+</html>

+ 117 - 0
hw2/script1.js

@@ -0,0 +1,117 @@
+//assign: evaluation 
+var a = 5;
+var b, c;
+
+//if we delete brackets functionality is same
+// b = (a * 5);
+// b = (c = b/2);
+// a = 5
+// b = 12,5
+// c = 12,5
+
+//one line variant
+c = b = (a * 5) / 2;
+
+// forgot one semicolon!
+// for(let i = 0 i < 5; i++) {
+//     console.log(i)
+// }
+// wrong usage of semicolon
+//let a; b = 5;
+//b = 5 c = b + 1;
+
+//semicolon logic errors
+// if(a === 5) {
+//     console.log('FIVE!')
+// }else; console.log('not five(')
+
+//GET YEAR TASK:
+function getAge(age, currentYear = new Date().getFullYear()) {
+    try { 
+        age = Math.trunc(Number(age))
+        if(!age || isNaN(age) || age > 200 || age < 0) {
+            throw e
+        } else {
+            currentYear = currentYear - age
+            return [currentYear, currentYear-1]
+        }
+    } catch(e) {
+        return 0
+    }
+}
+
+let year = getAge(prompt("input your age",""))
+if(year) {
+    alert(`Looks like you've been born in ${year[0]} or ${year[1]}`)
+} else {
+    alert('whoops, looks like invalid input value has been given!')
+}
+
+
+//GET TEMPERATURE TASK:
+try {
+    let grad = Number(prompt("Input temperature",""))
+    let sys = prompt(`
+        Type:
+            F - to translate to Fahrenheit
+            C - to translate to Celsius
+    `)
+    let res = getTemperature(grad, sys)
+    if(res) {
+        alert(`${grad}${sys.toLowerCase() === 'c'? '°F':'°C'} = ${res}${sys.toLowerCase() === 'c'? '°C':'°F'}`)
+    } else {
+        throw e;
+    }
+} catch(e) {
+    alert('invalid input!')
+}
+
+function getTemperature(value, system="c") {
+    if(isNaN(Number(value))) return false
+
+    switch(system.toLowerCase()) {
+        case 'f':
+            return ((value * 9/5) + 32).toFixed(2)
+        case 'c':
+            return ((value - 32) * 5/9).toFixed(2)
+        default:
+            return false
+    }
+}
+
+//divide using floor
+function flooredDivide(num1, num2) {
+    console.log(num1 / num2)
+    return Math.floor(num1 / num2)
+}
+
+//infor user if number given is odd task
+function oddInformer() {
+    let num = prompt("input a number:", "")
+    if(isNaN(+num) || !num) {
+        alert("not a number!!!")
+    } else {
+        if(num % 2 !== 0) {
+            alert(`${num} is odd`)
+        } else {
+            alert(`${num} is even`)
+        }
+    }
+}
+oddInformer()
+
+//greet user using alert() and prompt()
+function greeting() {
+    let name = prompt("Hey user, what's your name")
+    alert(`Hello, ${name? name : 'Namless One...'}`)
+}
+greeting()
+
+//check if text contains desired words 
+function lexis(string, word) {
+    if(string.indexOf(word) > 0) {
+        return true
+    } else {
+        return false
+    }
+}