Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 117,523 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,045 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



searching through and editing an array

 
Reply to this topicStart new topic

searching through and editing an array

cavvysri
post 21 Jun, 2008 - 06:18 AM
Post #1


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 29


My Contributions


Hi,

im trying to write code for someone to reserve a book, the books are stored in an array, and there are other arrays to show which books have been reserved or not..

the user will enter the index number of a book to reserve, the program then searches through the array and then reserves the book by editing the array so the book is no longer available.

im struggling with the code, can anyone give any pointers please?

CODE


var borrowedArray = ['-', '-', '-', 'rs-14', '-', '-'];
var reservedArray = ['-', 'ly-44', '-', '-', '-', '-'];
var readerNameArray = ['ly-44', 'mr-7', 'rs-14', 'al-15', 'pn-44', 'wk-46'];
var indexArray = ['0', '1', '2', '3', '4', '5'];

document.write('' + '<BR>' + 'What would you like to do?' + '<BR>')
                        document.write('1. Reserve a book' + '<BR>')
                        document.write('2. Borrow a book' + '<BR>')
                        readerIndex= window.prompt('Enter 1 or 2 to select','');
                        if (readerIndex == 1)
                            document.write('please enter the index number of the book you wish to reserve' + '<BR>')
                            readerIndex = 0;
                            readerIndex = window.prompt('Enter a number 0 - 6 to select','');
                            
                            
                            if (readerIndex > 6)
                            {
                                document.write('No such book!')
                            }
                            if (readerIndex[bookStatus] == 'reserved' || 'on loan')
                            {
                                document.write('you cannot reserve that book')
                            }
                            else
                            {
                                document.write('Book reserved')
                            }



i know this is completely wrong, as i think i need to use a for loop to loop through the array,
can anyone give an example of how to edit an array?
User is offlineProfile CardPM

Go to the top of the page


RodgerB
post 21 Jun, 2008 - 08:28 AM
Post #2


D.I.C Lover

Group Icon
Joined: 21 Sep, 2007
Posts: 2,038



Thanked 11 times

Dream Kudos: 2200
My Contributions


Here is an example of editing an array.

jscript

// Array of random words
var jsArray = ['lol', 'cat', 'meow'];

// Our loop
for(i = 0; i < jsArray.length; i++)
{
// Check to see if the array element i
// equals cat.
if(jsArray[i] == 'cat')
{
// If it does, blank it.
jsArray[i] = "";
}
}


Hope that helps. smile.gif
User is offlineProfile CardPM

Go to the top of the page

cavvysri
post 21 Jun, 2008 - 08:36 AM
Post #3


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 29


My Contributions


QUOTE(RodgerB @ 21 Jun, 2008 - 08:28 AM) *

Here is an example of editing an array.

jscript

// Array of random words
var jsArray = ['lol', 'cat', 'meow'];

// Our loop
for(i = 0; i < jsArray.length; i++)
{
// Check to see if the array element i
// equals cat.
if(jsArray[i] == 'cat')
{
// If it does, blank it.
jsArray[i] = "";
}
}


Hope that helps. smile.gif

Hi thanks for that, would it be the same for a function too? i.e. just changing the jsArray to the function name?
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 22 Jun, 2008 - 12:25 AM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,545



Thanked 61 times

Dream Kudos: 925
My Contributions


It would be something like so:

CODE
// Array of random words  
var jsArray = ['lol', 'cat', 'meow'];  
function  find(array, from, to){
  // Our loop  
  for(i = 0; i < array.length; i++)  {  
    // Check to see if the array element i  
    // equals cat.  
    if(array[i] == find){  
      // If it does, blank it.  
      jsArray[i] = to;  
    }  
  }  
  return array;
}

find(jsArray, 'cat', '');
// returns jsArray = ['lol', '', 'meow'];  


Hope that helps.
User is online!Profile CardPM

Go to the top of the page

cavvysri
post 22 Jun, 2008 - 01:44 AM
Post #5


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 29


My Contributions



[/quote]
almost there lol...what if i wanted the user to input an index, and the function to check if the index is a certain value, then edit that value

this is doing my head in, sorry for bein a pain
User is offlineProfile CardPM

Go to the top of the page

ahmad_511
post 22 Jun, 2008 - 11:04 PM
Post #6


D.I.C Regular

Group Icon
Joined: 28 Apr, 2007
Posts: 351



Thanked 8 times

Dream Kudos: 400
My Contributions


Hello,
first of, there is a small typo in BetaWare's function
QUOTE

if(array[i] == find){

replace (find) with (from)

now,the this function is already seach for a certain value (from) and if exists replaced with another value (to) within a specific Array (array)
so all you have to do is to prompt the user to input the search value and you pass this value through the function:
js

what=prompt("Serach for");
find(jsArray, what, 'xxxx'); // this will replace the user input (if exists) with "xxxx"

now if you're looking to edit value in other Array such as (reservedArray ) you can use the same search index for it if it has the same sequence of the search arrary (jsArray)
js

if(array[i] == from){
// If it does, blank it.
reservedArray [i] = to;
}


Good Luck
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 03:21PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month