/    Sign up×
Community /Pin to ProfileBookmark

please help me with my code

this programme is supposed to ask for you for a word no less than eight letters, then ask you for a numerical value which corresponds to a letter in the word and then the print your word the letter and how many times it appears.

what am I doing wrong !!!!

<HTML>
<HEAD> M 150 TMAO3 Question 1 (iii)(a)
<TITLE>Enter a number </TITLE>
<SCRIPT language=”JavaScript” >
var userInput;
var userNumber;
var upperLimit = 8;
var total;
var index;

userInput = window.prompt(‘Please enter a word with at least ‘ + upperLimit +’ letters’,”);
total = 0;
while (userInput.length < 8)
{
userInput = window.prompt(‘Please re-enter – word should be at least ‘ + upperLimit + ‘ letters’, ”)
}

userNumber = window.prompt(‘Please enter the number of the word’,”);
if (userNumber > userInput.length)

{
window.prompt(‘Please enter a number no greater than your word’,”)
userNumber = parseFloat(userNumber);
}
index = 0;
for (var count = 0; count < userInput.length; count = count + 1)
{
if (userInput.charAt(userNumber) == count)
{
total = total + 1
}
};

document.write(‘The number of occurences of is ‘ + total)

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@boskoJun 04.2007 — Change this:

(userInput.charAt(userNumber) == userInput.charAt(count))
Copy linkTweet thisAlerts:
@rudolfsunauthorJun 04.2007 — SO stupid why couldn't I see it!!!

One thing though it doesn't print what the letter is, how do I do that?
Copy linkTweet thisAlerts:
@mrhooJun 04.2007 — You are confusing character codes, which are numbers, with printable characters.

[CODE]var Limit = 8;
var total=0;
userInput = window.prompt('Please enter a word with at least ' + Limit +' letters','');
len = 0;
while (len < Limit ){
userInput= userInput .replace(/[^a-zA-Z]/g,'');
//replace non letters in input
len = userInput.length;
if(len < Limit ){
userInput = window.prompt('Please re-enter - word must be at least ' +
Limit + ' letters', userInput);
}
}

userNumber = parseFloat(window.prompt('Please enter the number position of a letter',''));
while(isNaN(userNumber) || userNumber==0 || userNumber > len){
// make sure input is a valid number
userNumber= parseFloat(window.prompt('Please enter a number between 1 and '+len,''));
}

userNumber= userInput.charAt(userNumber-1);
while(len>=0) if(userInput.charAt(len--)== userNumber) ++total;
document.write('The number of occurences of '+(userNumber)+' is ' + total );[/CODE]
Copy linkTweet thisAlerts:
@rudolfsunauthorJun 04.2007 — See this is a bit of homework I am stuck on, and I don't understand some of the functions you have posted, it's a bit advanced for a noob like me

but thanks
×

Success!

Help @rudolfsun spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...