/    Sign up×
Community /Pin to ProfileBookmark

Hello, new here and desperately trying to sort my javascript out! I’m at uni, and have never worked with javascript before. I’m trying to write a mini program to basically write out a code from an array (which I’ve done) but then select a section of that code and assign it a name… for example…

the codes are all something like this, but with varying amounts of numbers and letters before the ” – “…

3JJS12-ABC/876JD887

I need the javascript to select the ABC bit, determine whether or not it has A as its first letter and C as its last (there are always only 3 letters in that section of the code) then write out a name for it, such as code 1… I need to do this several times for different codes… and I haven’t got a clue how to go about it!! Anyone that can help?? ?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JMar 08.2007 — Post the code you have already done
Copy linkTweet thisAlerts:
@happy-beanyauthorMar 08.2007 — <html>

<head>Planner

<title>Process codes</title>

<script language="JavaScript" >

var numberData = ['13TR-ABC/784THG','554-CDE/UR9230','1D221-XYZ/490URT','9T9-JKL/34RW21'];

var firstNumber = '';

firstNumber = numberData[0];

for (count = 0; count <= firstNumber.length; count = count + 1)

{

};

document.write('<BR>' + 'Your number is ' + firstNumber + '<BR>')

</script>

</head>

<body>

</body>

</html>
Copy linkTweet thisAlerts:
@happy-beanyauthorMar 09.2007 — No ideas? ?
Copy linkTweet thisAlerts:
@crushmeguyMar 09.2007 — [CODE]
<script language="JavaScript" >
var numberData = ['13TR-ABC/784THG','554-CDE/UR9230','1D221-XYZ/490URT','9T9-JKL/34RW21'];
var s, isFirstLetterA, isLastLetterC;

for (var i = 0; i < numberData.length; i++){
s = numberData[i];
isFirstLetterA = s.charAt(s.indexOf('-') + 1) == 'A';
isLastLetterC = s.charAt(s.indexOf('-') + 3) == 'C';
if(isFirstLetterA && isLastLetterC){
//do what you want when both letters are present
}else if(isFirstLetterA){
//do what you want when only the first letter is 'A'
}else if(isLastLetterC){
//do what you want when only the last letter is 'C'
}else{
//do what you want when neither is present
}
}

</script>
[/CODE]

This code loops through your array and checks for the letters at positions relative to the first '-'. If there is a '-' before the true '-' you are looking for, the code will break. It will also break if any of the characters you are looking for are lower case.

If you dont understand the code, it basically finds the first index of the dash. It then returns the character one position away and sees if it is an 'A'. It then looks at the character three positions away to see if it is a 'C'.

Doug
Copy linkTweet thisAlerts:
@Tweak4Mar 09.2007 — How odd that the problem in this post is almost exactly the same as this one...

http://www.webdeveloper.com/forum/showthread.php?t=141051
Copy linkTweet thisAlerts:
@crushmeguyMar 09.2007 — I feel like I've been used!

Doug
Copy linkTweet thisAlerts:
@thechasboiMar 09.2007 — Not to be a moral upstandard but I never posted question to solve an entire problem in a forum. I have asked for ideas and asked if my ideas made sense but never in "uni" have I ever done this. Hope your prof does not catch you.
Copy linkTweet thisAlerts:
@happy-beanyauthorMar 09.2007 — Ahh I wasn't asking for the whole thing wrote out, just the bit that would tell me how to select a certain area (obviously indexOf!) which is why I didn't post the whole thing in the first place ?

Thanks for the reply, gutted that it now looks like I was trying to get someone to write the whole thing! I only posted the javascript because I thought that my first post didn't make sense ?
Copy linkTweet thisAlerts:
@thechasboiMar 10.2007 — .indexedof('what ever')>-1 send a bool
×

Success!

Help @happy-beany 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...