Is the number entered with javascript odd or even?
Solutions to sample questions that may appear in exams,
programming fundamentals course exam questions and solutions answers
Hello dear followers, I am your training coach produced by Artificial Intelligence, in this video we will solve simple problems in Javascript with you.
In this video of our video series, we will check whether the number from the user is odd or even.
We can use the text box, which is a form element, or the prompt structure, which is a javascript element, to get information from the user.
First of all, we will place a text box in our html structure.
We will connect the checking of the value entered in the text box to the click event of the button. For this, we add a normal button after the text box.
To write the javascript codes, we open the "script" tags in the html document.
We will first obtain the button within the "script" tags.
By defining a variable called my button, we get the only button in the document by typing "button" into the "querySelector" method of the "document" class.
We define a new function for the button's "addEventListener" method "click" event so that it does something when the button we get is clicked.
In the function we defined, we define a variable named text to get the text box.
We will use the “value” property to get the value of the text box.
We test the value we obtained with mod 2, as a result of testing with mod 2, if the value is divisible by 2, it will return 0, if not, it will return 1 value. As you know, even numbers are divisible by 2.
the if control structure; “even” if the remainder is 0, if the remainder is not 0; that is, if it is 1, we arrange it to give a "single" message.
Now we open our html document in the browser program to check the codes we have written. We run our tests by entering a few different values.
That's it.
Now it's your turn
You can improve the codes I wrote and offer alternatives. For example, you can input different data, output data or use shorter control structures.
Please write your solutions in the comments. I will definitely comment on what you wrote.
In the meantime, you can press the like button and subscribe to support and contribute to the coming of the videos. See you in the next video.