Monday, June 8, 2020

JavaScipt String function and features


String

String Quotes

Strings can be enclosed within either single quotes, double quotes.


Backtick quotes

  • process expression

  • process function

  • multi line


Special characters



Accessing characters

Notes:
The only difference between them is that if no character is found, [] returns undefined, and charAt returns an empty string.

iterate character by character



String length



Changing the case




Searching for a substring

str.indexOf

  • returns the position where the match was found
  • -1 if nothing can be found.

str.lastIndexOf(substr, position)
There is also a similar method str.lastIndexOf(substr, position) that searches from the end of a string to its beginning. It would list the occurrences in the reverse order.

includes

The more modern method str.includes(substr, pos) returns true/false depending on whether str contains substr within. It’s the right choice if we need to test for the match, but don’t need its position

str.startsWith and str.endsWith


Sunday, June 7, 2020

JavaScript Quiz Game 1

Requirement:

quiz.html
quiz.js
quiz.css

quiz.html file code:

<div id="quiz"></div>
<button id="submit">Get Results</button> 
<div id="results"></div>

quiz.css file code:

body{
font-size: 20px;
font-family: sans-serif;
color: #333;
}
.question{
font-weight: 600;
}
.answers {
margin-bottom: 20px;
}
#submit{
font-family: sans-serif;
font-size: 20px;
background-color: #297;
color: #fff;
border: 0px;
border-radius: 3px;
padding: 20px;
cursor: pointer;
margin-bottom: 20px;
}
#submit:hover{
background-color: #3a8;
}

quiz.js file code: 

var quizContainer = document.getElementById('quiz');
var resultsContainer = document.getElementById('results');
var submitButton = document.getElementById('submit');

var myQuestions = [
    {
        question: "What is 10/2?",
        answers: {
            a: '3',
            b: '5',
            c: '115'
        },
        correctAnswer: 'b'
    },
    {
        question: "What is 30/3?",
        answers: {
            a: '3',
            b: '5',
            c: '10'
        },
        correctAnswer: 'c'
    }
];


generateQuiz(myQuestions, quizContainer, resultsContainer, submitButton);
 
function generateQuiz(questions, quizContainer, resultsContainer, submitButton){   
  function showQuestions(questions, quizContainer){ 
	// we'll need a place to store the output and the answer choices
	var output = [];
	var answers;

	// for each question...
	for(var i=0; i<questions.length; i++){
		
		// first reset the list of answers
		answers = [];

		// for each available answer to this question...
		for(letter in questions[i].answers){

			// ...add an html radio button
			answers.push(
				'<label>'
					+ '<input type="radio" name="question'+i+'" value="'+letter+'">'
					+ letter + ': '
					+ questions[i].answers[letter]
				+ '</label>'
			);
		}

		// add this question and its answers to the output
		output.push(
			'<div class="question">' + questions[i].question + '</div>'
			+ '<div class="answers">' + answers.join('') + '</div>'
		);
	}

	// finally combine our output list into one string of html and put it on the page
	quizContainer.innerHTML = output.join('');
  }   function showResults(questions, quizContainer, resultsContainer){ 
	// gather answer containers from our quiz
	var answerContainers = quizContainer.querySelectorAll('.answers');
	
	// keep track of user's answers
	var userAnswer = '';
	var numCorrect = 0;
	
	// for each question...
	for(var i=0; i<questions.length; i++){

		// find selected answer
		userAnswer = (answerContainers[i].querySelector('input[name=question'+i+']:checked')||{}).value;
		
		// if answer is correct
		if(userAnswer===questions[i].correctAnswer){
			// add to the number of correct answers
			numCorrect++;
			
			// color the answers green
			answerContainers[i].style.color = 'lightgreen';
		}
		// if answer is wrong or blank
		else{
			// color the answers red
			answerContainers[i].style.color = 'red';
		}
	}

	// show number of correct answers out of total
	resultsContainer.innerHTML = numCorrect + ' out of ' + questions.length;
  }   // show the questions 
 showQuestions(questions, quizContainer); 

 // when user clicks submit, show results 
 submitButton.onclick = function(){ 
   showResults(questions, quizContainer, resultsContainer); 
  } 
}
Reference:
https://simplestepscode.com/
http://w3schools.com/
https://codewithawa.com/

PHP CRUD dengan MySQL database

Aplikasi: Daftar Kontak Sederhana, Buku Alamat

CRUD (Create, edit, update and delete) posts

Kebutuhan:

  • PHP
  • MySQL
    • database: crud.
    • table: info.
    • field:
      • id - int(11)
      • name - varchar(100)
      • address - varchar(100).
 index.php :

<!DOCTYPE html>
<html>
<head>
    <title>CRUD: CReate, Update, Delete PHP MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <form method="post" action="server.php" >
        <div class="input-group">
            <label>Name</label>
            <input type="text" name="name" value="">
        </div>
        <div class="input-group">
            <label>Address</label>
            <input type="text" name="address" value="">
        </div>
        <div class="input-group">
            <button class="btn" type="submit" name="save" >Save</button>
        </div>
    </form>
</body>
</html>
styles.css :

body {
    font-size: 19px;
}
table{
    width: 50%;
    margin: 30px auto;
    border-collapse: collapse;
    text-align: left;
}
tr {
    border-bottom: 1px solid #cbcbcb;
}
th, td{
    border: none;
    height: 30px;
    padding: 2px;
}
tr:hover {
    background: #F5F5F5;
}

form {
    width: 45%;
    margin: 50px auto;
    text-align: left;
    padding: 20px;
    border: 1px solid #bbbbbb;
    border-radius: 5px;
}

.input-group {
    margin: 10px 0px 10px 0px;
}
.input-group label {
    display: block;
    text-align: left;
    margin: 3px;
}
.input-group input {
    height: 30px;
    width: 93%;
    padding: 5px 10px;
    font-size: 16px;
    border-radius: 5px;
    border: 1px solid gray;
}
.btn {
    padding: 10px;
    font-size: 15px;
    color: white;
    background: #5F9EA0;
    border: none;
    border-radius: 5px;
}
.edit_btn {
    text-decoration: none;
    padding: 2px 5px;
    background: #2E8B57;
    color: white;
    border-radius: 3px;
}

.del_btn {
    text-decoration: none;
    padding: 2px 5px;
    color: white;
    border-radius: 3px;
    background: #800000;
}
.msg {
    margin: 30px auto;
    padding: 10px;
    border-radius: 5px;
    color: #3c763d;
    background: #dff0d8;
    border: 1px solid #3c763d;
    width: 50%;
    text-align: center;
}

Aplikasi To-do list Dengan PHP dan MySQL

Kebutuhan:

  • database MySQL: todo.
  • table: tasks.
  • Field: id - int(10), task - varchar(255).
  • File: index.php, style.css.

index.php file:

<?php
    // initialize errors variable
    $errors = "";

    // connect to database
    $db = mysqli_connect("localhost", "root", "", "todo");

    // insert a quote if submit button is clicked
    if (isset($_POST['submit'])) {
        if (empty($_POST['task'])) {
            $errors = "You must fill in the task";
        }else{
            $task = $_POST['task'];
            $sql = "INSERT INTO tasks (task) VALUES ('$task')";
            mysqli_query($db, $sql);
            header('location: index.php');
        }
    }
 
 
// delete task
if (isset($_GET['del_task'])) {
    $id = $_GET['del_task'];

    mysqli_query($db, "DELETE FROM tasks WHERE id=".$id);
    header('location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>ToDo List Application PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="heading">
        <h2 style="font-style: 'Hervetica';">ToDo List Application PHP and MySQL database</h2>
    </div>
    <form method="post" action="index.php" class="input_form">
<?php if (isset($errors)) { ?>
    <p><?php echo $errors; ?></p>
<?php } ?>
        <input type="text" name="task" class="task_input">
        <button type="submit" name="submit" id="add_btn" class="add_btn">Add Task</button>
    </form>

<table>
    <thead>
        <tr>
            <th>N</th>
            <th>Tasks</th>
            <th style="width: 60px;">Action</th>
        </tr>
    </thead>

    <tbody>
        <?php
        // select all tasks if page is visited or refreshed
        $tasks = mysqli_query($db, "SELECT * FROM tasks");

        $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
            <tr>
                <td> <?php echo $i; ?> </td>
                <td class="task"> <?php echo $row['task']; ?> </td>
                <td class="delete">
                    <a href="index.php?del_task=<?php echo $row['id'] ?>">x</a>
                </td>
            </tr>
        <?php $i++; } ?>   
    </tbody>
</table>

</body>
</html>

style.css file :

.heading{
    width: 50%;
    margin: 30px auto;
    text-align: center;
    color:     #6B8E23;
    background: #FFF8DC;
    border: 2px solid #6B8E23;
    border-radius: 20px;
}
form {
    width: 50%;
    margin: 30px auto;
    border-radius: 5px;
    padding: 10px;
    background: #FFF8DC;
    border: 1px solid #6B8E23;
}
form p {
    color: red;
    margin: 0px;
}
.task_input {
    width: 75%;
    height: 15px;
    padding: 10px;
    border: 2px solid #6B8E23;
}
.add_btn {
    height: 39px;
    background: #FFF8DC;
    color:     #6B8E23;
    border: 2px solid #6B8E23;
    border-radius: 5px;
    padding: 5px 20px;
}

table {
    width: 50%;
    margin: 30px auto;
    border-collapse: collapse;
}

tr {
    border-bottom: 1px solid #cbcbcb;
}

th {
    font-size: 19px;
    color: #6B8E23;
}

th, td{
    border: none;
    height: 30px;
    padding: 2px;
}

tr:hover {
    background: #E9E9E9;
}

.task {
    text-align: left;
}

.delete{
    text-align: center;
}
.delete a{
    color: white;
    background: #a52a2a;
    padding: 1px 6px;
    border-radius: 3px;
    text-decoration: none;
}
Referensi:
https://codewithawa.com/
https://www.bitdegree.org/

Saturday, June 6, 2020

CSS left panel layout sample

HTML code:

<!--Force IE6 into quirks mode with this comment tag-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSS Left Panel Layout</title>
<link rel="stylesheet" href="index.css"/>

<script type="text/javascript">
/*** Temporary text filler function. Remove when deploying template. ***/
var gibberish=["Ular melingkar di atas pagar.", "Satu Ribu, Dua Biru, Tiga Ribu, Empat Biru.", "Kelapa di Parut, Kepala di Garuk."]
function filltext(words){
for (var i=0; i<words; i++)
document.write(gibberish[Math.floor(Math.random()*3)]+" ")
}
</script>

</head>

<body>

<div id="leftpanel">
<div class="inner-content">

<h1>CSS Left Panel Layout</h1>
<h3>Sample text here</h3>

</div>
</div>


<div id="main-content">
<div class="inner-content">

<h2>Right Panel Title</h2>
<p><script type="text/javascript">filltext(125)</script></p>
<p style="text-align: center">Footer: <a href="#">CSS Left Panel Layout</a></p>

</div>
</div>


</body>
</html>

CSS code:

/* liquid layout with the left column being static, always in view. Tested and works in IE5+, Opera7+, and Firefox. */
body{
margin: 0;
padding: 0;
border: 0;
overflow: hidden;
height: 100%;
max-height: 100%;
}

#leftpanel{
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 200px; /*Width of left div*/
height: 100%;
overflow: hidden; /*Disable scrollbars. Set to "scroll" to enable*/
background: #f0f0f0;
color: black;
}

#main-content{
position: fixed;
top: 0;
left: 200px; /*Set left value to Width Of Left Div*/
right: 0;
bottom: 0;
overflow: auto;
background: #fff;
}

.inner-content{
margin: 15px; /*Margins for inner DIV inside each DIV (to provide padding)*/
}

* html body{ /*IE6 hack*/
padding: 0 0 0 200px; /*Set value to (0 0 0 WidthOfFrameDiv)*/
}

* html #maincontent{ /*IE6 hack*/
height: 100%;
width: 100%;
}

Reference:
http://www.dynamicdrive.com/
https://trinket.io/

Friday, June 5, 2020

How to insert fraction in Microsoft Word

Method 1a using Microsoft Word field

Fraction equation in Microsoft Word of field:
{EQ \f(x, y)}

Just make sure you insert regular field braces by pressing Ctrl+F9, and replace x with your numerator number and y with your denominator number.

Method 1b using Microsoft Word field

Fraction equation in Microsoft Word of field:
{ EQ \s\up2(9)/\s\do2(11) }

Just make sure you insert regular field braces by pressing Ctrl+F9, and replace x with your numerator number and y with your denominator number. The result is a fraction using a horizontal divider between the numerator and the denominator.

The result look like this:
1/2

Method 2 using Equation Editor 3.0

requirement:
Equation Editor 3.0 objects will still display normally if you have MT Extra font installed (if you don't have the font, you can search on search engine and download it, or you can download it here https://www.microsoft.com/en-us/download/details.aspx?id=56828).

In Microsoft Office Word 2003 and may some before version:

  1. In Word, place the insertion point where you want the math symbol or template to insert.
  2. From the Insert menu, select Object...
  3. The Object dialog box appears.
    • Windows: From the Object type scroll box, select Microsoft Equation 3.0
    • Macintosh: From the Object type scroll box, select Microsoft Equation
  4. Click OK
    • Windows: The Equation Editor and Equation toolbar appear.
    • Macintosh: The Equation dialog box appears containing the Equation Editor and Equation toolbar.
  5. Use the fraction on the Equation toolbar or dialog box to add fraction to your document.
  6. Finish.

In Microsoft Office Word 2007 and new version:

  1. Click Insert tab, in the Text group, click Object.
  2. In the Object type box, click Microsoft Equation 3.0, and then click OK.
  3. Use the fraction on the Equation toolbar to add fraction to the equation. If you finish, then to return to your document, click anywhere in the document.
  4. Finish.

Method 3 using built-in equation

  1. Choose Insert > Equation
  2. then click "Inset New Equation".
  3. After you insert the equation then Equation Tools Design tab opens with symbols and structures that can be added to your equation.
  4. Then add your requirement fraction on Fraction group in Ribbon.
  5. Finish
Reference:
https://www.teachucomp.com/
https://www.uwec.edu/
https://word.tips.net/
https://www.myofficetricks.com/
https://support.microsoft.com/

Related Topic:

Wednesday, June 3, 2020

VBA Word Document Object 1

Dim variable_name As Document

' binding variable name object to document object
Set variable_name = ActiveDocument

Set variable_name = ThisDocument

Set variable_name = Documents("file name.extension")
' or
Set variable_name = Documents("file name.extension")

' the first document in the Documents collection.
Set variable_name = Documents(1)