Basic Tutorial Javascript : Learn To Program To Determine Odd Number Or Even Number

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • You will learn to create a form and combine it with javascript
  • You will learn arithmetic operators
  • You will learn to program to determine odd and even numbers
  • You will learn the use of javascript in programming

Requirements

  • You already have basic HTML knowledge
  • You must have a text editor and browser to learn this tutorial
    Difficulty
  • Basic
    Tutorial Contents
    In this tutorial I will discuss the use of arithmetic operators to create a program to determine even and odd numbers, so consider this tutorial carefully.
  • Create a page with html extension, its example: odd.html
  • Write the basic html element code command to begin filling in the code, which we will run in conjunction with the javascript code. Here's the code
< html>
< head >
< title > learn to program to determine odd number or even number < /tilte>
< /head >
< body >
< /body >
< /html >
Next, before starting the program we will generate < SCRIPT language = "Javascript"> < / script> function to start and introduction of programming language which will be executed and put after < head> and before < /head>
  • now we will create a function test variable and a value to start the arithmetic operator, following the code
function test () {
var val1 = document.harjuky.T1.value
add the code under < / script>

code explanation:
var val1 = document.harjuky. is the variable name
value is the value of the variable

  • then create an arithmetic operator, as for this command its function to determine an even or odd number number, following the command
if (val1% 2 == 0)
after we create the variable now just add the command to display the correct answer option.
document.harjuky.T2.value = "even number"

explanation:
if we enter the number of mass with the value of 2 then the answer is an even number, so if we enter for example the number 3, then the answer is an odd number.
Here's the code:

document.harjuky.T2.value = "odd number"
but do not forget to add else to his command then end with . More complete code :
< SCRIPT language = "Javascript">
function test () {
var val1 = document.harjuky.T1.value
if (val1% 2 == 0)
document.harjuky.T2.value = "even number"
else
document.harjuky.T2.value = "odd number"
}
< /SCRIPT>
- at this stage we have managed to create javascript code, then we will form to run its function, as well as form to fill in the numbers. - Before the < body> tag put the code < form method = "POST" name = "harjuky"> command to send the POST command and process the arithmetic operator. Then make the input form numbers and form the results using HTML code.
< p> Number < input type = "text" name = "T1" size = "20">
is the number < input type = "text" name = "T2" size = "20">
< /p>
< p> < input type = "button" value = "CHECK" name = "B1" onclick = test ()>
< /p>
- After that close the form with < /form>
  • Then save and run in the browser, if all the code you write is correct then the result as below
    odd.gif

Full Code :


< html>
< head>< title > learn to program to determine odd number or even number < /tilte>
< SCRIPT language = "Javascript">
function test (){
var val1=document.harjuky.T1.value
if (val1%2==0)
document.harjuky.T2.value = "even number"
else
document.harjuky.T2.value = "odd number"
}
 < /SCRIPT>
 < /head>
< body>
< form method="POST" name="harjuky">
< p>Number < input type = "text" name = "T1" size = "20">
is the number < input type = "text" name = "T2" size = "20">
< /p>
< p> < input type = "button" value = "CHECK" name = "B1" onclick =test()>
< /p>
< /form>
< /body>
< /html>

Live Code : Codepen.Io
Github : odd.html
Curriculum
Next Tutorial



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @jestemkioskiem, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

thanks bro

Remember JS evaluates numbers as booleans as C does, so the == 0 part in the if sentence is not needed. If you still want to use it, do it a triple equals (===) instead.

You can better write a simple function as:

function isOdd(n) {
  return n % 2;
}

isOdd(5)  // returns true
isOdd(4)  // returns false

Thank you for your advice

Hey @harjuky I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x