Please help a javascript idiot

This is a discussion on "Please help a javascript idiot" within the JavaScript Forum section. This forum, and the thread "Please help a javascript idiot are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jun 18th, 2007, 11:00
New Member
Join Date: Jun 2007
Location: uk
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Please help a javascript idiot

Could anyone help me amend this simple program.
I have to complete the following assignment ASAP and don't have a clue how:

Create 3 parallel arrays to store 15 contacts names, addresses and phone numbers.
Create a function to prompt the user for the contact details.
Create a function to print the details.
In the main part of the program the user is repeatedly asked whether they want to add a new contact or end the program. Add the necessary codes and call the functions as appropriate.

This is what I managed to do so far:
Code: Select all
//program to store contacts details

var max = 15
//create and initialize three parallel arrays
var names = new Array(max)
var address = new Array(max)
var telephone = new Array(max)

//function to add new contacts
function contacts()
{
for(var i = 0; i < max; i++){            
names[i] = prompt('Please enter the name here','')
address[i] = prompt('Please enter the address here','')
telephone[i] = prompt('Please enter the telephone number here','')}
};


//function to print contacts details
function print()
{
for(var i = 0; i < max i++){
document.write(    names[i] + '<BR>'+ address[i] + '<BR>' + telephone[i] + '<BR>' + '<BR>')}
};


//program starts here
var add
add = window.prompt('Do you want to add a new contact? (yes/no)?','')
if (add == 'yes'){
contacts()}

print()
The problem is the user is supposed to add a contact and then be asked if they wish to add a new one and so on, for 15 times. Whereas the program i created asks the user 15 times consecutively to add details and then prompts whether they wish to carry on. I tried to use a while loop instead but didn't work either.
I hope I made myself understood. i would really appreciate any suggestion

Last edited by karinne; Jun 18th, 2007 at 13:39. Reason: Please use [code]...[/code] tags when displaying code!
Reply With Quote

  #2 (permalink)  
Old Jun 18th, 2007, 12:24
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Please help a javascript idiot

Your problem is that the for loop is in the contacts function, it needs to be down to where you prompt "Do you want to add...". I myself don't have that much clue of javascript, but here's my (untested) version:
Code: Select all
//program to store contacts details

var max = 15;
//create and initialize three parallel arrays
var names = new Array(max);
var address = new Array(max);
var telephone = new Array(max);

//function to add new contacts
function getContact(index) {
 names[index] = prompt('Please enter the name here','');
 address[index] = prompt('Please enter the address here','');
 telephone[index] = prompt('Please enter the telephone number here','');
}

//function to print contacts details
function print()
{
 for(var i = 0; i < max; i++) {
  document.write( names[i] + '<BR>'+ address[i] + '<BR>' + telephone[i] + '<BR>' + '<BR>');
 }
}


//program starts here
for(var i = 0; i < max; i++){
 var add;
 add = window.prompt('Do you want to add a new contact? (yes/no)?','')
 if (add == 'yes') {
  getContact(i);
 } else {
  break; // so that the user doesn't have to say no 15 times if he wants to quit
 }

print();
Try that.
Reply With Quote
  #3 (permalink)  
Old Jun 18th, 2007, 20:27
New Member
Join Date: Jun 2007
Location: uk
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Please help a javascript idiot

Thank you so much c010depunkk.
Finally the program works fine, the only problem is that I'm not allowed to use the 'break statement' because this is something my tutor hasn't covered yet, so I need to find an alternative way to stop the program.

THANK YOU AGAIN!
Reply With Quote
  #4 (permalink)  
Old Jun 18th, 2007, 20:50
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Please help a javascript idiot

No problem. What kind of a course are you taking?
Reply With Quote
  #5 (permalink)  
Old Jun 18th, 2007, 21:08
New Member
Join Date: Jun 2007
Location: uk
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Please help a javascript idiot

Thank you for replying so quckly. I'm taking a javascript course for beginners covering only the very basic such as while, if and for loops, simple functions, simple arrays.
In this particular homework I need to use parallel arrays, 2 functions, and conditional statements.

You are very kind
Reply With Quote
Reply

Tags
beginner, javascript

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Idiot question about documents kingwilliam Starting Out 2 May 23rd, 2008 16:10
Poll: Is Moojoo an Idiot? moojoo Webforumz Cafe 44 Jan 4th, 2007 20:33
what does + + mean in Javascript eg: var m = (A1+ +B4); Andy K JavaScript Forum 6 Dec 13th, 2006 23:57
Question about User loggins from an idiot Webstuck PHP Forum 6 Nov 27th, 2006 22:29
Its the coding idiot =p PHP_newb Introduce Yourself 6 Jul 22nd, 2005 08:05


All times are GMT. The time now is 19:28.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43