0002 added[Accept userinput, typerconversion]

This commit is contained in:
WickedJack99
2022-07-12 00:04:16 +02:00
parent 0fd73d5ea2
commit 3a08b730a2
2 changed files with 40 additions and 1 deletions

View File

@@ -11,6 +11,10 @@
<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>
<!--Accept User Input at html document-->
<label id="myLabel">Enter your name</label><br>
<input type="text" id="myText"><br>
<button type="button" id="myButton">submit</button>
<script src="/js/index.js"></script>
</body>
</html>

View File

@@ -111,4 +111,39 @@ console.log(students);
//--------------------------------------------------------------------------------------------------------------
//8. Accept user input
//https://youtu.be/8dWL3wF_OMw?t=1288
//8.1 Easy way via prompt. Not very practical. Care that user input is at string datatype,
//so typeconversion may be needed.
//let username = window.prompt("What's your name?");
//console.log(username);
//8.2
let username;
document.getElementById("myButton").onclick = function() {
username = document.getElementById("myText").value;
console.log(username);
document.getElementById("myLabel").innerHTML = username;
}
//--------------------------------------------------------------------------------------------------------------
//9. Typeconversion
//Change the datatype of a value to another. (strings, numbers, boolean)
let age3 = "23";
console.log(typeof age3);
age3 = Number(age3);
age3++;
console.log(age3);
console.log(typeof age3);
let y = String(3.14);
console.log(y);
console.log(typeof y);
//Useful to check if user entered some required input.
let z = Boolean(""); //empty string is converted to false
console.log(z);
console.log(typeof z);
z = Boolean("z"); //not empty string is converted to true
console.log(z);
console.log(typeof z);
//--------------------------------------------------------------------------------------------------------------
//10. const keyword
//https://youtu.be/8dWL3wF_OMw?t=1812