This is the python code im trying to convert into javascript.
JavaScript
x
15
15
1
ime = input("Enter your name: nn")
2
3
# correction made by AmyHubbertx
4
for c in ime:
5
6
c = c.upper()
7
if (c == "A"):
8
print("..######..n..#....#..n..######..n..#....#..n..#....#..nn")
9
elif (c == "B"):
10
print("..######..n..#....#..n..#####...n..#....#..n..######..nn")
11
elif (c == "C"):
12
print("..######..n..#.......n..#.......n..#.......n..######..nn")
13
elif (c == "D"):
14
print("..#####...n..#....#..n..#....#..n..#....#..n..#####...nn")
15
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?
JavaScript
1
12
12
1
/*function yourName()*/
2
let name = prompt("Enter Your Name")
3
4
if (i === 'a' || 'A'){
5
document.write(("..######..<br>..#........#..<br>..######..<br>..#........#..<br>..#........#..<br><br>"))
6
} if ( i = 'b' || 'B'){
7
document.write(("..######..<br>..#........#..<br>..#####...<br>..#........#..<br>..######..<br><br>"))
8
} if ( i = 'c' || 'C'){
9
document.write(("..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br><br>"))
10
}
11
/*}*/
12
Advertisement
Answer
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)
JavaScript
1
13
13
1
const name = prompt("Enter Your Name", "CAB").trim().toUpperCase()
2
let text = "No name given";
3
4
const chars = {
5
'A': "..######..<br>..#....#..<br>..######..<br>..#....#..<br>..#....#..<br>",
6
'B': "..######..<br>..#....#..<br>..#####...<br>..#....#..<br>..######..<br>",
7
'C': "..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br>",
8
}
9
10
if (name.length > 0) {
11
text = name.split("").map(c => chars[c] || "X<br/>"); // return the character or X if not found
12
document.getElementById("content").innerHTML = `<pre>${text.join("</pre><pre>")}</pre>`;
13
}
JavaScript
1
1
1
pre {display: inline-block; float: left }
JavaScript
1
1
1
<div id="content"></div>