I have this python code that I am trying to turn into javascript but I cant seem to figure out which if statement. to use
Question:
This is the python code im trying to convert into javascript.
ime = input("Enter your name: nn")
# correction made by AmyHubbertx
for c in ime:
c = c.upper()
if (c == "A"):
print("..######..n..#....#..n..######..n..#....#..n..#....#..nn")
elif (c == "B"):
print("..######..n..#....#..n..#####...n..#....#..n..######..nn")
elif (c == "C"):
print("..######..n..#.......n..#.......n..#.......n..######..nn")
elif (c == "D"):
print("..#####...n..#....#..n..#....#..n..#....#..n..#####...nn")
I want to be able to write anything and get each letter written to the page. This current code writes ‘a’ even if you put the wrong letter.
How can I input something and have it write to the page?
/*function yourName()*/
let name = prompt("Enter Your Name")
if (i === 'a' || 'A'){
document.write(("..######..<br>..#........#..<br>..######..<br>..#........#..<br>..#........#..<br><br>"))
} if ( i = 'b' || 'B'){
document.write(("..######..<br>..#........#..<br>..#####...<br>..#........#..<br>..######..<br><br>"))
} if ( i = 'c' || 'C'){
document.write(("..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br><br>"))
}
/*}*/
Answers:
Your if
condition is wrong.
if (i === 'a' || i === 'A') {
is what you need, otherwise the statement reads:
if i
is equal to ‘a’ OR ‘A’
Instead of
if i
is equal to ‘a’ OR i
is equal to ‘A’
Because you aren’t comparing the second part to anything it will return true.
Many issues. =
is assignment and ==
or ===
is comparison just like in Python.
Also not you should NEVER use document.write
after the page has loaded. It will wipe the page.
Here is a working version taking all letters you type (as long as they are A, B, or C)
const name = prompt("Enter Your Name", "CAB").trim().toUpperCase()
let text = "No name given";
const chars = {
'A': "..######..<br>..#....#..<br>..######..<br>..#....#..<br>..#....#..<br>",
'B': "..######..<br>..#....#..<br>..#####...<br>..#....#..<br>..######..<br>",
'C': "..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br>",
}
if (name.length > 0) {
text = name.split("").map(c => chars[c] || "X<br/>"); // return the character or X if not found
document.getElementById("content").innerHTML = `<pre>${text.join("</pre><pre>")}</pre>`;
}
pre {display: inline-block; float: left }
<div id="content"></div>
This is the python code im trying to convert into javascript.
ime = input("Enter your name: nn")
# correction made by AmyHubbertx
for c in ime:
c = c.upper()
if (c == "A"):
print("..######..n..#....#..n..######..n..#....#..n..#....#..nn")
elif (c == "B"):
print("..######..n..#....#..n..#####...n..#....#..n..######..nn")
elif (c == "C"):
print("..######..n..#.......n..#.......n..#.......n..######..nn")
elif (c == "D"):
print("..#####...n..#....#..n..#....#..n..#....#..n..#####...nn")
I want to be able to write anything and get each letter written to the page. This current code writes ‘a’ even if you put the wrong letter.
How can I input something and have it write to the page?
/*function yourName()*/
let name = prompt("Enter Your Name")
if (i === 'a' || 'A'){
document.write(("..######..<br>..#........#..<br>..######..<br>..#........#..<br>..#........#..<br><br>"))
} if ( i = 'b' || 'B'){
document.write(("..######..<br>..#........#..<br>..#####...<br>..#........#..<br>..######..<br><br>"))
} if ( i = 'c' || 'C'){
document.write(("..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br><br>"))
}
/*}*/
Your if
condition is wrong.
if (i === 'a' || i === 'A') {
is what you need, otherwise the statement reads:
if
i
is equal to ‘a’ OR ‘A’
Instead of
ifi
is equal to ‘a’ ORi
is equal to ‘A’
Because you aren’t comparing the second part to anything it will return true.
Many issues. =
is assignment and ==
or ===
is comparison just like in Python.
Also not you should NEVER use document.write
after the page has loaded. It will wipe the page.
Here is a working version taking all letters you type (as long as they are A, B, or C)
const name = prompt("Enter Your Name", "CAB").trim().toUpperCase()
let text = "No name given";
const chars = {
'A': "..######..<br>..#....#..<br>..######..<br>..#....#..<br>..#....#..<br>",
'B': "..######..<br>..#....#..<br>..#####...<br>..#....#..<br>..######..<br>",
'C': "..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br>",
}
if (name.length > 0) {
text = name.split("").map(c => chars[c] || "X<br/>"); // return the character or X if not found
document.getElementById("content").innerHTML = `<pre>${text.join("</pre><pre>")}</pre>`;
}
pre {display: inline-block; float: left }
<div id="content"></div>