neilbigh101 06-15-2007, 10:08 AM Good morning to all, i`m trying to change the code so that instead of displaing the days as numbers i`d like to change it to names, been trying with code very kindly provided by PhillipM (fortunatley made a copy of it) but screen doesn`t display anthing by the time i`ve finished messing with it any help would be much appreicated Neil
<html>
<head>
<title>Arrays</title>
<script type = "text/javascript">
/* Program to read in a known number of data and store them in an array */
var bookArray = new Array (5);
var highestNum = 0;
var highestDay = 0;
document.write(' Array to read in a number of data items');
for (var day = 0; day <bookArray.length; day = day + 1) {
bookArray[day] = window.prompt('Enter books value for day ' + day,'')
// Update highestNum and highestDay if highestNum is greater than previous value
if (bookArray[day] > highestNum) {
highestNum = parseInt(bookArray[day]); // number not string
highestDay = day;
}
}
document.write('<BR>' + '<BR>');
document.write('confirmation of data input' + '<BR>' + '<BR>');
for (var day = 0; day < bookArray.length; day = day + 1) {
document.write(bookArray[day] + '<BR>');
}
document.write("<BR>" + "The highest number of books was " + highestNum + " on day " + highestDay);
</script>
Do you mean something like this?
<script type = "text/javascript">
/* Program to read in a known number of data and store them in an array */
var days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
var bookArray = new Array (5);
var highestNum = 0;
var highestDay = 0;
onload=function test(){
document.getElementById("display").innerHTML='Array to read in a number of data items';
for(var dayNum = 0; dayNum<bookArray.length; dayNum++){
bookArray[dayNum] = window.prompt('Enter books value for day ' + days[dayNum],'')
// Update highestNum and highestDay if highestNum is greater than previous value
if (bookArray[dayNum] > highestNum) {
highestNum = parseInt(bookArray[dayNum]); // number not string
highestDay = days[dayNum];
}
}
document.getElementById("display").innerHTML+='<BR>' + '<BR>';
document.getElementById("display").innerHTML+='confirmation of data input' + '<BR>' + '<BR>';
for(var dayNum = 0; dayNum < bookArray.length; dayNum++) {
document.getElementById("display").innerHTML+=bookArray[dayNum] + '<BR>';
}
document.getElementById("display").innerHTML+="<BR>" + "The highest number of books was " + highestNum + " on " + highestDay;
}
</script>
<div id="display"></div>
Philip M 06-15-2007, 02:43 PM I'm not sure that this chap has got as far as innerHTML yet.
And surely the first day of the week is Sunday, not Monday?
I think what he was looking for at this stage was more like:-
<html>
<head>
<title>Arrays</title>
<script type = "text/javascript">
/* Program to read in a known number of data and store them in an array */
var dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var bookArray = new Array (6);
var highestNum = 0;
var highestDay = 0;
document.write(' Array to read in a number of data items');
for (var day = 1; day <bookArray.length; day = day + 1) {
bookArray[day] = window.prompt(' enter books value for day ' + day,'')
if (isNaN(bookArray[day])) {
alert ("Invalid value!")
}
if (bookArray[day] > highestNum) {
highestNum = parseInt(bookArray[day]); // number not string
highestDay = day;
}
}
document.write('<BR>' + '<BR>');
document.write('confirmation of data input' + '<BR>' + '<BR>');
for (var day = 1; day < bookArray.length; day = day + 1) {
document.write(bookArray[day] + '<BR>');
}
document.write("<BR>" + "The highest number of books was " + highestNum + " on day " + highestDay + " which is " + dayNames[highestDay]);
</script>
</head>
<body>
</body>
</html>
neilbigh101 06-15-2007, 06:00 PM hi Philip, i`ve copied and run it but it still gives the day`s as number values not actual names your help once again would be much appreciated best regards Neil
Philip M 06-15-2007, 06:40 PM Well, it works for me.
An example of the output:-
Array to read in a number of data items
confirmation of data input
25
12
54
34
17
The highest number of books was 54 on day 3 which is Wednesday
neilbigh101 06-15-2007, 10:59 PM hi Philip, very sorry your dead right, i was expecting it to display the day names in the dialouge box on top of the window, can that be changed so that the user see`s it also? as alway`s amazed at your knowledge, something i can only aspire to (still having fun with the other codes you so kindly posted, i will get there honest, well i hope so) thank`s again Neil
Philip M 06-16-2007, 07:54 AM bookArray[day] = window.prompt('Enter books value for day ' + day + " which is " + dayNames[day],'')
Can I very respectfully point out that the apostrophe before an s in English normally means possession - e.g. Philip's book (which is a contraction of 'Philip his book').
Plurals just take an s (no apostrophe). E.g. Many thanks!
But it's with an apostrophe is a contraction of 'it is' - e.g. It's a book!
Its without an apostrophe is possessive - e.g. Always include its parameters.
Sorry to mention this, but accuracy in language is important as well as in JavaScript. It can often make a big difference to the meaning.
Consider: Those weird-looking things over there are my husbands (husband's).
I once saw a sign for Fi'sh and Chip's!
neilbigh101 06-16-2007, 04:43 PM Think I better put down for English in September as well? thanks again Neil
And surely the first day of the week is Sunday, not Monday?
Yep my mistake, I suppose my post does deserve to be ignored
|
|