Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why won't the if statements work? 3

Status
Not open for further replies.

hitsuin

Vendor
Aug 23, 2010
24
GB
Hi Everyone, I need anyone who can to show me what I'm doing wrong and how to fix it.

I want the 5th character input into this page to display differently. Also, if anyone can tell me why firefox keeps working on the page like its incomplete, which it probably is, but why, I'd really appreciate it.

The code goes like this:

Code:
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var code=prompt("Please enter your code");

if ( code[4] = A ) { code[4] = S ; }
if ( code[4] = B ) { code[4] = M ; }
if ( code[4] = C ) { code[4] = I ; }
if ( code[4] = D ) { code[4] = L ; }
if ( code[4] = E ) { code[4] = E ; }


document.write(code);
document.write("<br />");
}

</script>
</head>
<body>

<input type="button" onclick="show_prompt()" value="code" />

</body>
</html>
OR

Code:
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var code=prompt("Please enter your code");

if ( code[4] = 0 ) { code[4] = 9 ; }
if ( code[4] = 1 ) { code[4] = 8 ; }
if ( code[4] = 2 ) { code[4] = 7 ; }
if ( code[4] = 3 ) { code[4] = 6 ; }
if ( code[4] = 4 ) { code[4] = 5 ; }


document.write(code);
document.write("<br />");
}

</script>
</head>
<body>

<input type="button" onclick="show_prompt()" value="code" />

</body>
</html>

The second one goes farther but not all the way, please help me out.
 
Several things:

1. This: var code=prompt("Please enter your code"); returns a single value, you are treating it as an array later on for no apparent reason.

2. This: if ( code[4] = 0 ) { code[4] = 9 ; }
There is no comparison here, what you are saying is: If you can set the array named "code"'s index number 4 to 0 then set it to 9.

What you want to say is if the value of index 4 is x cange it to Y then:

if ( code[4] =[red]=[/red] 0 ) { code[4] = 9 ; }


What is code[4] supposed to be? The prompt returns simply a variable"code" with a number value. Yet you are trying to use it as an array.

What exactly is it you want to do?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks Phil AKA Vacunita, that does help. I thanked you officaially too. What I really want to do is to be able to type in any word and have the javascript change any or all of the characters in the word to whatever I want. Do you have any ideas as to how I can do that?
 
You can use the replace() method of a string in conjunction with the charAt() method for instance:

Code:
if(code.charAt(4)=='0'){ code=code.replace(code.charAt(4),"9");

In the string "555505555" It would change the 0 for a 9. So the output would be: 5555[red]9[/red]5555

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi

Phil said:
What is code[4] supposed to be?
It is the 4[sup]th[/sup] character of the string. But [tt]code[teal][[/teal][purple]4[/purple][teal]][/teal] [teal]=[/teal] [purple]9[/purple][/tt] is absolutely wrong as the reference by index is a getter, so assignment is not supported.
Code:
[COLOR=darkgoldenrod]alert[/color][teal]([/teal] [green][i]'Vacunita'[/i][/green][teal][[/teal][purple]4[/purple][teal]][/teal] [teal])[/teal] [gray]// 'n'[/gray]
The above code alerts 'n' in Gecko, Presto, WebKit and KHTML. But it is not mentioned by the ECMAScript standard, so probably Trident does not know it.

And sorry, I can not agree with your suggestion of using the [tt]replace()[/tt] method. It does regular expression based replacement, so will replace the first occurrence of the given character, not necessarily the desired one :
Code:
code[teal]=[/teal][green][i]'555505555'[/i][/green]
[b]if[/b] [teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]3[/purple][teal])==[/teal][green][i]'5'[/i][/green][teal])[/teal] code[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]3[/purple][teal]),[/teal][green][i]"9"[/i][/green][teal]);[/teal]
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]code[teal])[/teal] [gray]// 955505555[/gray]


Feherke.
 
Yes I know what it means, as I said above I knew he was attempting to use it as an array. In Javascript that doesn't work. So I was asking for clarifcation in case he was attempting something else.

As far as the replace method, you are right, it does only change the first occurrence, in which case I guess turning the string into an array and working off of that may be a better alternative.

Code:
var code=prompt("Please enter your code");

var mycode=new Array();
for(var i=0; i<=code.length-1; i++){
mycode[i]=code.charAt(i);

}


if(mycode['4']=="0"){ mycode['4']=="9"; }
...


var newcode="";
for(var x in mycode){

newcode+=mycode[x];

}

document.write(newcode);

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
@Vacunita

---------------
if(code.charAt(4)=='0'){ code=code.replace(code.charAt(4),"9");

--------------

Just doesn't work for some reason, the page is blank.

@Feherke

Seeing as you disagree with Vacunita what do you suggest and why?
 
You're missing a closing brace :

Code:
if (code.charAt(4) == '0') [!]{[/!] code = code.replace(code.charAt(4), "9"); [!]}[/!]

Given you don't need the opening brace, why not simply remove it?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
I can't do it, I'm still lost in the wilderness. I guess I'm too much of a novice, but I'm going to officially thank you guys all for trying.
 
Hi

Phil said:
as I said above I knew he was attempting to use it as an array.
No. Writing an index enclosed in brackets ( [] ) after the variable/value does not necessary means it is used as an array. Strings also support that notation.
Phil said:
In Javascript that doesn't work.
It works in JavaScript, but not in JScript.
hitsuin said:
Seeing as you disagree with Vacunita what do you suggest and why?
If you have many such replacements to do, Vacunita's array-based suggestion is the fastest :
Code:
[b]var[/b] code[teal]=[/teal][COLOR=darkgoldenrod]prompt[/color][teal]([/teal][green][i]"Please enter your code"[/i][/green][teal]);[/teal]

[b]var[/b] mycode[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]''[/i][/green][teal]);[/teal]

[b]if[/b] [teal]([/teal]mycode[teal][[/teal][purple]4[/purple][teal]]==[/teal][green][i]'0'[/i][/green][teal])[/teal] mycode[teal][[/teal][purple]4[/purple][teal]]=[/teal][green][i]'9'[/i][/green][teal];[/teal]

[b]var[/b] newcode[teal]=[/teal]mycode[teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]''[/i][/green][teal]);[/teal]

document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal]newcode[teal]);[/teal]
But talking about many replacements, better store that data in a structured way. So adding more replacement pairs will affect only the data structure, not the code :
Code:
[b]var[/b] tr[teal]=[/teal][teal]{[/teal]
  [green][i]'4'[/i][/green][teal]:[/teal][teal]{[/teal] [green][i]'A'[/i][/green][teal]:[/teal][green][i]'S'[/i][/green][teal],[/teal] [green][i]'B'[/i][/green][teal]:[/teal][green][i]'M'[/i][/green][teal],[/teal] [green][i]'C'[/i][/green][teal]:[/teal][green][i]'I'[/i][/green][teal],[/teal] [green][i]'D'[/i][/green][teal]:[/teal][green][i]'L'[/i][/green][teal],[/teal] [green][i]'E'[/i][/green][teal]:[/teal][green][i]'E'[/i][/green] [teal]}[/teal]
[teal]}[/teal]

[b]var[/b] code[teal]=[/teal][COLOR=darkgoldenrod]prompt[/color][teal]([/teal][green][i]"Please enter your code"[/i][/green][teal]);[/teal]

[b]var[/b] mycode[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]''[/i][/green][teal]);[/teal]

[b]for[/b] [teal]([/teal][b]var[/b] char [b]in[/b] tr[teal])[/teal]
  [b]if[/b] [teal]([/teal]mycode[teal][[/teal]char[teal]][/teal] [b]in[/b] tr[teal][[/teal]char[teal]])[/teal] mycode[teal][[/teal]char[teal]]=[/teal]tr[teal][[/teal]char[teal]][[/teal]mycode[teal][[/teal]char[teal]]];[/teal]

[b]var[/b] newcode[teal]=[/teal]mycode[teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]''[/i][/green][teal]);[/teal]

document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal]newcode[teal]);[/teal]
But for just a few replacements [tt]substring()[/tt] would be enough :
Code:
[b]var[/b] code[teal]=[/teal][COLOR=darkgoldenrod]prompt[/color][teal]([/teal][green][i]"Please enter your code"[/i][/green][teal]);[/teal]

[b]if[/b] [teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]4[/purple][teal])==[/teal][green][i]'A'[/i][/green][teal])[/teal] code[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]4[/purple][teal])+[/teal][green][i]'S'[/i][/green][teal]+[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]5[/purple][teal]);[/teal]
[b]if[/b] [teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]4[/purple][teal])==[/teal][green][i]'B'[/i][/green][teal])[/teal] code[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]4[/purple][teal])+[/teal][green][i]'M'[/i][/green][teal]+[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]5[/purple][teal]);[/teal]
[b]if[/b] [teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]4[/purple][teal])==[/teal][green][i]'C'[/i][/green][teal])[/teal] code[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]4[/purple][teal])+[/teal][green][i]'I'[/i][/green][teal]+[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]5[/purple][teal]);[/teal]
[b]if[/b] [teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]4[/purple][teal])==[/teal][green][i]'D'[/i][/green][teal])[/teal] code[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]4[/purple][teal])+[/teal][green][i]'L'[/i][/green][teal]+[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]5[/purple][teal]);[/teal]
[b]if[/b] [teal]([/teal]code[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal][purple]4[/purple][teal])==[/teal][green][i]'E'[/i][/green][teal])[/teal] code[teal]=[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal][purple]4[/purple][teal])+[/teal][green][i]'E'[/i][/green][teal]+[/teal]code[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]5[/purple][teal]);[/teal]

document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal]code[teal]);[/teal]
Which is still to much code, so a [tt]function[/tt] would be nice :
Code:
[b]function[/b] [COLOR=darkgoldenrod]tr[/color][teal]([/teal]str[teal],[/teal]char[teal],[/teal]from[teal],[/teal]to[teal])[/teal]
[teal]{[/teal]
  [b]if[/b] [teal]([/teal]str[teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal]char[teal])==[/teal]from[teal])[/teal] [b]return[/b] str[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal]char[teal])+[/teal]to[teal]+[/teal]str[teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal]char[teal]+[/teal][purple]1[/purple][teal]);[/teal]
  [b]return[/b] str[teal];[/teal]
[teal]}[/teal]

[b]var[/b] code[teal]=[/teal][COLOR=darkgoldenrod]prompt[/color][teal]([/teal][green][i]"Please enter your code"[/i][/green][teal]);[/teal]

code[teal]=[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal]code[teal],[/teal][purple]4[/purple][teal],[/teal][green][i]'A'[/i][/green][teal],[/teal][green][i]'S'[/i][/green][teal]);[/teal]
code[teal]=[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal]code[teal],[/teal][purple]4[/purple][teal],[/teal][green][i]'B'[/i][/green][teal],[/teal][green][i]'M'[/i][/green][teal]);[/teal]
code[teal]=[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal]code[teal],[/teal][purple]4[/purple][teal],[/teal][green][i]'C'[/i][/green][teal],[/teal][green][i]'I'[/i][/green][teal]);[/teal]
code[teal]=[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal]code[teal],[/teal][purple]4[/purple][teal],[/teal][green][i]'D'[/i][/green][teal],[/teal][green][i]'L'[/i][/green][teal]);[/teal]
code[teal]=[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal]code[teal],[/teal][purple]4[/purple][teal],[/teal][green][i]'E'[/i][/green][teal],[/teal][green][i]'E'[/i][/green][teal]);[/teal]

document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal]code[teal]);[/teal]
But I would prefer a method instead :
Code:
String[teal].[/teal][b]prototype[/b][teal].[/teal]tr[teal]=[/teal][b]function[/b][teal]([/teal]char[teal],[/teal]from[teal],[/teal]to[teal])[/teal]
[teal]{[/teal]
  [b]if[/b] [teal]([/teal][b]this[/b][teal].[/teal][COLOR=darkgoldenrod]charAt[/color][teal]([/teal]char[teal])==[/teal]from[teal])[/teal] [b]return[/b] [b]this[/b][teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal]char[teal])+[/teal]to[teal]+[/teal][b]this[/b][teal].[/teal][COLOR=darkgoldenrod]substring[/color][teal]([/teal]char[teal]+[/teal][purple]1[/purple][teal]);[/teal]
  [b]return[/b] [b]this[/b][teal];[/teal]
[teal]}[/teal]

[b]var[/b] code[teal]=[/teal][COLOR=darkgoldenrod]prompt[/color][teal]([/teal][green][i]"Please enter your code"[/i][/green][teal]);[/teal]

code[teal]=[/teal]code
  [teal].[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal][purple]4[/purple][teal],[/teal][green][i]'A'[/i][/green][teal],[/teal][green][i]'S'[/i][/green][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal][purple]4[/purple][teal],[/teal][green][i]'B'[/i][/green][teal],[/teal][green][i]'M'[/i][/green][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal][purple]4[/purple][teal],[/teal][green][i]'C'[/i][/green][teal],[/teal][green][i]'I'[/i][/green][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal][purple]4[/purple][teal],[/teal][green][i]'D'[/i][/green][teal],[/teal][green][i]'L'[/i][/green][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]tr[/color][teal]([/teal][purple]4[/purple][teal],[/teal][green][i]'E'[/i][/green][teal],[/teal][green][i]'E'[/i][/green][teal]);[/teal]

document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal]code[teal]);[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top