Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 Input, in which are inputed numbers. after click on Check button it should show me how many couple numbers exist and which are those. For example i input 2 and 9, it must show me "There are 4 couple numbers (2,4,6,8)".

What I have tried:

<input type="text" id="tiv1" placeholder="Number 1">
		<input type="text" id="tiv2" placeholder="Number 2">
		<button onclick="check()">Check</button>
		<h1 id="result"></h1>



function check(){
	var a = Number(document.getElementById("tiv1").value);
	var b = Number(document.getElementById("tiv2").value);
	var count = 0;
	for(var i = a; i <= b; i++){
		if(i % 2 == 0){
			count++;
			document.getElementById("result").innerHTML = "There are " + count + " couple numbers " + " ( " + i + ")";
		}
	}
}



But in this case instead of "There are 4 couple numbers(2,4,6,8)", it shows me "There are 4 couple numbers (8)".
What's the problem?
Posted
Updated 5-Jul-18 21:08pm

First of all what is couple number? I am guessing you are talking about even number.

Anyway, If you write on innerHTML on a loop then you will get only the final result.

function check(){
	var a = Number(document.getElementById("tiv1").value);
	var b = Number(document.getElementById("tiv2").value);
	var count = 0;
        var evenNUmbers = new Array();
	for(var i = a; i <= b; i++){
		if(i % 2 == 0){
			count++;
			evenNUmbers.push(i);
		}
	}
        document.getElementById("result").innerHTML = "There are " + count + " couple numbers " + " ( " + evenNUmbers.join() + ")";
}
 
Share this answer
 
Solution:
function check() {
            var a = Number(document.getElementById("Text1").value);
            var b = Number(document.getElementById("Text2").value);
            var count = 0;var number = "";
            for (var i = a; i <= b; i++) {
                if (i % 2 == 0) {
                    count++;
                    number += i + ",";
                   }
               } 
               document.getElementById("result").innerHTML = "There are " + count + " couple numbers " + " ( " + number + ")";
            
        }
 
Share this answer
 
Comments
Suren97 6-Jul-18 2:58am    
Thank you very much :)
Sarita Mall 6-Jul-18 2:59am    
You are most welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900