Hey I'm only new to Javascript so bare with with me please, but I have to write code that will add a number say for example 1, to a set amount of numbers lets say 5, so for example the output of the program should be:

1+1=2
1+2=3
1+3=4
1+4=5
1+5=6.

I have programmed this in C however i am having problems doing this in Javascript, any help is greatly appreciated.

here is the code I have tried to write myself:

function main()
{
    
	
num1 ; 
var b =  number() 
num2[0] = 1 ; 
num2[1] = 2 ; 
num2[3] = 3 ; 
num2[4] = 4 ; 

i ; 

total ; 
	
document.write("enter a value for num1", num1);
	
for (i=0;i<20;i++)
{
document.write("enter a value for num2]", num[2]);
	
total = num1+ num2[i] ; 
	
	
	
	total= num1+num2[i]; 
	
	document.write("sum is equal to :", +total);
	
}

and the C code that works is :

//
//  main.m
//  Untitled
//
//  Created by Adam Johnston on 01/05/2010.
//  Copyright DIT 2010. All rights reserved.
//

#include <stdio.h>

int main()
{
    
	
	int number1 ; 
	int number2[20] ;
	int i ; 
	int total ; 
	
	printf("enter a number for value1\n");
	scanf("%d",&number1);
	
	for (i=0;i<20;i++)
	{
		printf("enter value 2\n");
		scanf("%d", &number2[i]) ;
		 
		number1+ number2[i] ; 
	
	
	
	total= number1+number2[i]; 
	
	printf("sum is equal to %d\n", total);
	}
}

You are confused between two separate environments. You can not ask user to input data in between. Javascript may access values from html input elements.

Member Avatar for rajarajan2017
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
 <script type="text/javascript">
function processfn()
{
	var num = parseInt(document.frm1.inp1.value);
	var limit  = parseInt(document.frm1.inp2.value);
	for (i=1;i<=limit;i++)
	{
		document.write(num + "+" + i + "= " + (num+i));
		document.write("<br/>");
	}
}
</script>
</head>

<body>
<form name="frm1" method="post" action="">
Add a Number: <input type="text" name="inp1" value="10"/>
Add a Limit: <input type="text" name="inp2" value="20"/>
<input type="submit" name="commit" value="Submit" onClick="processfn()"/>
</form>
</body>
</html>
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
 <script type="text/javascript">
function processfn()
{
	var num = parseInt(document.frm1.inp1.value);
	var limit  = parseInt(document.frm1.inp2.value);
	for (i=1;i<=limit;i++)
	{
		document.write(num + "+" + i + "= " + (num+i));
		document.write("<br/>");
	}
}
</script>
</head>

<body>
<form name="frm1" method="post" action="">
Add a Number: <input type="text" name="inp1" value="10"/>
Add a Limit: <input type="text" name="inp2" value="20"/>
<input type="submit" name="commit" value="Submit" onClick="processfn()"/>
</form>
</body>
</html>

Worked!! Only thing is the results from the form disappear after abut a second!

okay so now i have to do this : 2. Ask the user to select ODD or EVEN. If they select ODD your code from step 2 should print the sum and product of all odd numbers up to the user provided number. If they select EVEN your code should do likewise for the even numbers.

Member Avatar for rajarajan2017

Please give a try for yourself, and If any probs post the code

Logic:

i) Create two radio buttons (ODD EVEN)
ii) The code is like above and you are going to receive one more thing which was selected
iii) Then built the logic

Hope it helps! Reply if you not solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.