...

View Full Version : Help with charAt (i think)



homer_213
12-17-2007, 09:56 PM
Complete newbie here and with javascript. Having to write a small program, within this the user has to put in a password. Once ok is clicked it needs to be displayed, with only the 1st and last character showing with the rest replaced with an asterisk *. Ive got it so it adds one * but am stuck.

Can anyone help at all? Thanks

kansel
12-17-2007, 10:09 PM
It might help if you post what you have so far.

I would use charAt() to return the first character (0) and the last character (string.length-1). Display (string.length-2) asterisks in between them.

homer_213
12-17-2007, 10:22 PM
document.write('<BR>' + 'Password: ' + passwordLength.charAt(0) + character + passwordLength.charAt(length + 1))

Thats the line what ive got to display the first and last character and then display my variable 'character', which i defined as *. Does that help. What it needs to do is display the rest of the password, as its got to be at least 7 characters and less than 15! Does that make sense!

Trinithis
12-17-2007, 10:51 PM
passwordLength.charAt(passwordLength.length + 1)

charAt(length + n) does not exist for n >= 0

You probably meant to write

passwordLength.charAt(passwordLength.length - 1)

homer_213
12-17-2007, 11:06 PM
I meant to put that but as im new its not going to be exactly right. The thing is the program runs and does what i want it to do, except for the problem i had in my first post.

Philip M
12-18-2007, 09:34 AM
Here's another way:-

<script type = "text/javascript">

var pwd = "abcdefghi";
var pwdlen = pwd.length;
if (pwdlen <7 || pwd.length > 15) {
alert ("Invalid password! Must be 7 - 15 characters");
return false;
}

var pwd1 = pwd.match(/^./); //first character
var pwd3 = pwd.match(/.$/); // last character
var pwd2 = pwd.replace (/./g, "*"); // change to asterisks
var pwd2 = pwd2.replace(/\*\*/, ""); // delete two asterisks
var xpwd = pwd1 + pwd2 + pwd3; // concatenate password

alert (xpwd); // delete after testing

</script>

homer_213
12-18-2007, 09:36 AM
Thanks for the replies, i can understand where youre going. Forgot to say originally, that got to use the basics really, so really IF statements, loops, etc, does that make sense.

Philip M
12-18-2007, 09:58 AM
Well, here is yet another way:-

<script type = "text/javascript">

function obscure() {
var pwd = "abcdefghi";
var pwdlen = pwd.length;
var xpwd = "";
if (pwdlen <7 || pwd.length > 15) {
alert ("Invalid password! Must be 7 - 15 characters");
return false;
}

var a = pwd.charAt(0); // first character
var z = pwd.charAt(pwdlen-1); // last character

for (var i = 1; i < pwdlen-1; i++) {
xpwd += "*"; // change intermediate characters to *
}
xpwd = a + xpwd + z; // concatenate first + *** + last
alert (xpwd); // delete after testing
}

</script>

You must call the function by a button or by using onchange = "obscure()" in the password input field.

homer_213
12-18-2007, 09:06 PM
I kind of get what that does, but still cant use functions. Basically the question says using your ability to use conditionals, repetition structures and string handling? Does that narrow it down, thanks, sorry not being much help am i?

Philip M
12-18-2007, 09:44 PM
You should not ask us to do your homework for you. It amounts to cheating your tutor, your fellow-students and ultimately yourself. Are you sure that you have paid sufficient attention in class?

Let this suffice:-

<script type = "text/javascript">

var pwd = "abcdefghij";
var pwdlen = pwd.length;
var xpwd = "";
if (pwdlen <7 || pwd.length > 15) { // conditional
alert ("Invalid password! Must be 7 - 15 characters");
}

var a = pwd.charAt(0); // first character string handling
var z = pwd.charAt(pwdlen-1); // last character

for (var i = 1; i < pwdlen-1; i++) { // repetition structure
xpwd += "*"; // change intermediate characters to *
}
xpwd = a + xpwd + z; // concatenate first + *** + last // string handling
alert (xpwd); // delete after testing

</script>

homer_213
12-19-2007, 12:06 AM
Im not asking you to do it for me, just merely asking for advice. This is only part of the program that im stuck on. Just need a push in the right direction to see where i need to go, thats all.

Thanks Phil, was having a look earlier and thats the kind of thing i was aiming towards, at least i now where i need to look in my text!:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum