Skip to content
Advertisement

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

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>"))
    }
/*}*/

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)

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>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement