[英]I have an error in my school javascript project
我的代碼中有多個錯誤。 當我運行代碼時,它給出了一個錯誤,指出有一個意外的標識符。 這是完整的錯誤,“語法錯誤:/script.js:46:16 處的意外標識符”。 但是,當我檢查第 46 行到第 16 行時,我找不到任何未關閉的函數或方法。 當我在第 46 行評論 if 語句時,它只是在另一個 if 語句上給出錯誤。 有人能幫我嗎?
繼承人的代碼:
function print(str){ console.log(str) } function farhToKelvin(far){ const cel = Math.floor(far / (9/5) - 32) const kel = cel + 273 return kel } function farhToCelsius(far){ const cel = Math.floor(far / (9/5) - 32) return cel } function convertToFarh(type, val){ type.toLowerCase() val.toLowerCase() if (type == "kelvin"){ return Math.floor(far / (9/5) - 32 - 273) } else if(type == "celsius"){ return Math.floor(far / (9/5) - 32) } } while (farh:= "exit"){ var farh = prompt("enter a farhanhite tempature, ") var type = prompt("convert it to celsius. or kelvin") type,toLowerCase() if (type == "celsius"){ const c = farhToCelsius(farh) var val = convertToFarh(c) if (val > 50 or val == 50){ print("it is cold, you should wear a coat") } if (val > 50 or val == 50){ print("it is hot, you should wear a tank top") } } else if(type == "kelvin"){ const k = farhToKelvin(farh) var val = convertToFarh(k) if (val > 50 or val == 50){ print("it is cold, you should wear a coat") } if (val > 50 or val == 50){ print("it is hot, you should wear a tank top") } } }
if (val > 50 or val == 50){
在 Javascript 中,我們使用||
代替or
。 . 如果您再次遇到類似問題,您可能想看看JavaScript 中的這個符號是什么意思?
改變
if (val > 50 or val == 50)
對此
if (val > 50 || val == 50)
或者更好
if (val >= 50)
第 50 行和第 60 行和第 64 行也有類似的問題
你需要更新它們。 邏輯上第 46 行和第 50 行是相同的。 根據您的打印消息,第 50 行應該是 if (val > 50) 但第 45 行應該是 if (val <= 50)
所以你的代碼中有語法和語義問題需要解決
以下是您的代碼中存在的問題的完整列表:
or
代替||
(正如其他人所說)convertToFarh
中使用不正確的變量名 functionconvertToFarh
時省略type
參數number
變量上調用String.toLowerCase()
fahrenheit
拼寫錯誤or
代替||
( 邏輯或) 在您的代碼中,我相信您打算指示if (val > 50 || val == 50) {
而不是使用or
as used in other programming languages。
前:
if (val > 50 or val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 or val == 50){
print("it is hot, you should wear a tank top")
}
后:
if (val > 50 || val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 || val == 50){
print("it is hot, you should wear a tank top")
}
這個邏輯也不無道理。 也許你的意思是第一個value < 50
而第二個val >= 50
?
您還要重復自己是否要轉換為開爾文或攝氏度,以便可以將代碼提取到它自己的 function 中,或者只是減少if..else
塊以僅影響必要的變量並在這些塊之后執行比較.
convertToFarh
中使用不正確的變量名 function 在convertToFarh
function 中,您引用了一個名為far
的變量,但沒有該名稱的變量。 所以你要么是指引用val
參數,要么是試圖引用在 function 之外聲明的fahr
變量。在我的代碼中,我假設前者是val
的情況,並將其重命名如下:
function convertToFarh(type, val){
type.toLowerCase()
val.toLowerCase()
if (type == "kelvin"){
return Math.floor(val / (9/5) - 32 - 273)
}
else if(type == "celsius"){
return Math.floor(val / (9/5) - 32)
}
}
convertToFarh
時省略type
參數在 function 對convertToFarh
的兩次調用中,您使用c
或k
變量作為val
參數的值,但沒有指明類型。 我已修復此問題以指示每個部分的類型:
var val = convertToFarh("celsius", c);
var val = convertToFarh("kelvin", k);
number
變量上調用String.toLowerCase()
在convertToFarh
function 中,您試圖對number
類型 ( val
) 調用String.toLowerCase()
方法,這會出錯。 在我的代碼中,我只是將其注釋掉並確認它可以安全地刪除。
fahrenheit
拼寫錯誤這可能看起來沒什么大不了的,但是當其他人審查您的代碼(無論是錯誤修復還是一般代碼審查)時,確保變量具有正確的拼寫會有所幫助。 我已將代碼中的 function 名稱、變量名稱和任何對fahrenheit
的引用修復為正確的拼寫。 這包括:
"enter a fahrenheit temperature: "
function fahrToKelvin
和 function 電話function fahrToCelsius
和 function 電話function convertToFahr
和 function 調用farh
的變量為fahr
far
的參數更改為val
以避免變量名沖突 function print(str) { console.log(str); } function fahrToKelvin(val) { const cel = (val - 32) / (9 / 5); return Math.floor(cel + 273.15); } function fahrToCelsius(val) { const cel = Math.floor((val - 32) * 5 / 9); return cel; } function convertToFahr(type, val) { if (type == "kelvin") { return Math.floor(val / (5 / 9) - 459.67); } else if (type == "celsius") { return Math.floor(val / (9 / 5) + 32); } } var fahr = prompt("enter a fahrenheit tempature: "); var type = prompt("convert it to celsius, or kelvin"); type = type.toLowerCase(); if (type == "celsius") { const c = fahrToCelsius(fahr); var val = convertToFahr("celsius", c); if (val < 50) { print("it is cold, you should wear a coat"); } if (val >= 50) { print("it is hot, you should wear a tank top"); } } else if (type == "kelvin") { const k = fahrToKelvin(fahr); var val = convertToFahr("kelvin", k); if (val < 50) { print("it is cold, you should wear a coat"); } if (val >= 50) { print("it is hot, you should wear a tank top"); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.