Sesion 9
JavaScript


E#1 Type Of

Look at the console (F12).


Type of, it's an general fuction, to comprobate the type of a data, or variable

Variable

It's a received memoty to save data, it's defined with let or var, ECMAC recomend let, so here i'll use let


Now, look at the console

                        <script>
                            let myVar1 = "Hello";
                            let myVar2 = 123;

                            console.log("typeof myVar1: "+typeof(myVar1))
                            console.log("typeof myVar2: "+typeof(myVar2))
                        </script>
                    

E#2 Print my name



                        <script>
                            function printMyName(){
                                let name = prompt("Hello whats your name: ");

                                document.getElementById("nameOfUser").innerHTML = "Hello! Welcom " + name;
                            }
                        </script>
                    

E#3 Switching variables

Javascript Variables Exercise
Given the existing code below, can you write some code so that their values are switched around?

var a = "3";
var b = "8";

So that the variable a holds the value "8".
And the variable b holds the value "3".
When the code is run, it should output:

a is 8

b is 3

Do NOT change any of the existing code.
You are NOT allowed to type any numbers.
You should NOT redeclare the variables a and b.
Hint: Use this code playground to run your code and see if it matches your expectations.
Hint: The solution is just 3 lines of code.




E#4 Using length


                    <script>
                    //Code of
                    function lenCharacters(){
                        let text = document.getElementById("userInputText");
                        const max = 150;

                        //Using length
                        document.getElementById("results").innerHTML = ("Count: "+ text.value.length +
                            " Left: -"+ (max - text.value.length) + " (max=150)"
                        )
                    }
                    </script>
                    

E#5 Formating text


Using toUpperCase, toLowerCase

                    <script>
                    //Code of
                    function capitalize(){
                        let name = prompt("Type your name: ");
                        let nameUppercase = name.toUpperCase();
                        let nameLowercase = name.toLowerCase();
                        let nameCapitalice = name.toUpperCase().slice(0,1) + name.toLowerCase().slice(1,);

                        alert(
                            "Input: " + name + "\n\n"+
                            "name.toUpperCase():" + name.toUpperCase() + "\n"+
                            "name.toLowerCase():" + name.toLowerCase() + "\n"+
                            "Capitalice name:" + name.toUpperCase().slice(0,1) + name.toLowerCase().slice(1,)
                        )
                    }
                    </script>
                    

E#6 Dog age to human age

Fórmula Human Age = (dogAge -2) *4 +1

                    <script>
                    //Code of
                    function getHumanAge(){
                        let dogAgeText = document.getElementById("dogAge");
                        let dogAge = Number(dogAgeText.value.slice(9,));

                        if (typeof(dogAge) !== typeof(1)) {
                            dogAge = Number(dogAgeText.value);
                        }
                        
                        let humanAgeText = document.getElementById("humanAge");
                        let humanAge;
                        
                        if (typeof(dogAge) == typeof(1)) {
                            if (dogAge > 0) {
                                humanAge = (Number(dogAge) -2) * 4 + 1;
                            }else{
                                humanAge = 0;
                            };
                        }else{
                            humanAge = 0;
                        };

                        if (humanAge < 0) {humanAge = humanAge*-1;};

                        humanAgeText.value = "Human Age: "+ humanAge;
                        dogAgeText.value = "Dog Age: "+ dogAge;
                        
                        alert(
                            "Human Age aprox is: " + humanAge  + "\n\n"
                        );
                    }

                    </script>