簡體   English   中英

在 JS 中出現意外的 Token Var 錯誤

[英]Getting an Unexpected Token Var Error in JS

問題
我試過在這里查看其他解決方案,但似乎對我沒有任何作用。 我的問題是我在第 43 行得到了一個意外的 var 標記,但我找不到任何未閉合的方括號或圓括號。 我試過逐行刪除,但問題只從那一行開始。 如果我刪除第 43 行,它上面的行有一個意外的“)”標記,我也無法弄清楚。 這是代碼

代碼

 // FUNCTION DEFINITION(S) function map(array, callbackFunction) { var newArray = []; for (var i = 0; i < array.length; i++) { newArray = newArray + callbackFunction(element); } return newArray; } function cubeAll(numbers) { return map(numbers, function(n) { return n * n * n; }); } // ASSERTION FUNCTION(S) TO BE USED function assertArraysEqual (actual, expected, testName) { var allValuesAreEqual = true for (x = 0; x < actual.length; x++) { var actualValues = actual[x]; var expectedValues = expected[x]; if (actualvalues !== expectedValues) { allValuesAreEqual = false break; } } if (allValuesAreEqual === true) { console.log('passed') } else { console.log ('FAILED [' + testName + '] expected ' + expected + ', but got ' + actual + '.') } } // TESTS CASES var numbers = [2, 3, 4]; var output = function cubeAll(numbers) var actual = function map(output, cubeAll)

看起來您正在嘗試在測試用例中運行函數,但您使用的是function關鍵字,它定義了函數。

function關鍵字在下一個語句之前需要一個函數定義(例如{somecode()} ),但它不是找到{ ,而是在下一行找到關鍵字var

解決方案

var output = cubeAll(numbers)
var actual = map(output, cubeAll)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM