[英]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
這是我試圖轉換為 javascript 的 python 代碼。
ime = input("Enter your name: \n\n")
# correction made by AmyHubbertx
for c in ime:
c = c.upper()
if (c == "A"):
print("..######..\n..#....#..\n..######..\n..#....#..\n..#....#..\n\n")
elif (c == "B"):
print("..######..\n..#....#..\n..#####...\n..#....#..\n..######..\n\n")
elif (c == "C"):
print("..######..\n..#.......\n..#.......\n..#.......\n..######..\n\n")
elif (c == "D"):
print("..#####...\n..#....#..\n..#....#..\n..#....#..\n..#####...\n\n")
我希望能夠寫任何東西並將每封信寫到頁面上。 即使您輸入了錯誤的字母,此當前代碼也會寫入“a”。
如何輸入內容並將其寫入頁面?
/*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>"))
}
/*}*/
你的if
條件是錯誤的。
if (i === 'a' || i === 'A') {
是你需要的,否則語句如下:
if
i
is equal to 'a' OR 'A' 而不是 ifi
is equal to 'a' ORi
is equal to 'A'
因為您沒有將第二部分與將返回 true 的任何內容進行比較。
很多問題。 =
是賦值, ==
或===
是比較,就像在 Python 中一樣。
也不是你不應該在頁面加載后使用document.write
。 它會擦除頁面。
這是 JavaScript 與您的 Python 代碼非常相似。
let c = prompt("Enter Your Name","").trim().toUpperCase() let text = "No name given"; if (c === 'A') text = "..######..<br>..#........#..<br>..######..<br>..#........#..<br>..#........#..<br><br>"; else if (c === 'B') text = "..######..<br>..#........#..<br>..#####...<br>..#........#..<br>..######..<br><br>"; else if (c === 'C') text = "..######..<br>..#.......<br>..#.......<br>..#.......<br>..######..<br><br>"; document.write(text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.