2012-12-28 34 views
3

我嘗試了很多方法......我在做什麼錯?我決心學習,真正理解這一點。如何打印到屏幕 - 「console.log不是一個功能」

//On line 2, declare a variable myName and give it your name. 
var myName = "Jeanne"; 
//On line 4, use console.log to print out the myName variable. 
console.log ("Jeanne"); 
//On line 7, change the value of myName to be just the first 2 letters of your name. 
myName.substring(0, 2); 
//On line 9, use console.log to print out the myName variable; 
console.log("Jeanne"); 
+0

我一直得到的錯誤是:TypeError:console.log不是一個函數,這部分是我困惑的原因。 – tooheymomster

+0

如果使用IE瀏覽器:http://stackoverflow.com/questions/690251/what-happened-to-console-log-in-ie8,http://stackoverflow.com/questions/5472938/does-ie9-support-console -log-and-is-it-a-real-function/5473193#5473193(如果你使用的是不同的瀏覽器/環境,則酌情搜索) – 2012-12-28 07:41:45

回答

1

你需要assign來將子串的結果返回給變量來得到子串的結果。使用該變量來打印substring result

Live Demo

myName = myName.substring(0, 2); 
console.log(myName); 
1

可能是你的錯誤是,你每次收到 「珍妮」 在屏幕上。

這是因爲你打印的是恆定的,而不是你的變量。嘗試用這個打印:

myName = myName.substring(0, 2); 
console.log (myName); 

而且在其他的答案說 - 確保你使用的是支持的console.log瀏覽器

+0

@tooheymomster那麼,這將很容易解決*如果將這些信息包含在帖子*。我懷疑這是因爲舊版本的IE正在被使用.. – 2012-12-28 07:40:10

1

你這樣做是正確的只要登錄處理的變量,而不是原始名稱,完成!

//On line 2, declare a variable myName and give it your name. 
var myName = "Jeanne"; 
//On line 4, use console.log to print out the myName variable. 
console.log (myName); 
//On line 7, change the value of myName to be just the first 2 letters of your name. 
myName=myName.substring(0, 2); 
//On line 9, use console.log to print out the myName variable; 
console.log(myName); 
1

除其他事項外,請確保您使用的是支持「的console.log」的瀏覽器。這意味着安裝了Firebug插件的Chrome或FireFox,或開發人員工具的Internet Explorer 。然後打開開發者工具。對於Chrome,它是ctrl-shift-I;對於其他所有的東西都是F12。

+0

我正在使用FireFox。 – tooheymomster

+0

在這種情況下,請確保安裝Firebug。 –

0

字符串不可變,所以你應該把結果賦給一個新的變量。

2

請確保您使用的是支持console.log(火狐Firebug的,谷歌瀏覽器),如果您有您的瀏覽器的問題,嘗試在的jsfiddle

EDIT運行代碼瀏覽器

我試圖在jsfiddle和我的瀏覽器(Chrome和IE9)中運行你的代碼,它們都能很好地工作。

根據我讀過的評論,您使用的是Firefox。如果是這種情況,請確保安裝了Firebug插件。你可以得到它here

0

謝謝大家的幫助!我非常感謝您能夠提供並且知識淵博的回答問題。我弄明白了。顯然問題是與子字符串。我已經完整地輸入了字串子字符串。我想它不是那樣,只需要縮寫substr。

//On line 2, declare a variable myName and give it your name. 
var myName = "Jeanne"; 
//On line 4, use console.log to print out the myName variable. 
console.log (myName); 
//On line 7, change the value of myName to be just the first 2 
//letters of your name. 
myName = myName.substr(0,2); 
//On line 9, use console.log to print out the myName variable; 
console.log (myName); 
1

中序的執行console.log打印出您的VAR值,你應該包括在的console.log

這是在下面的例子中所示的paraenthesis之間的變量;

//在第2行中,聲明一個變量myName並將其命名。 var myName =「Jeanne」;

//在第4行中,使用console.log輸出myName變量。 console.log(myName);

+0

這不是問的問題。 – FazoM

0

我有console.log is not a function錯誤,並花了15分鐘的時間進行調試。原來我有一個名爲console的本地變量!使用window.console.log("...")做了伎倆。

0

想必你指定別的東西來CONSOLE.LOG,這在過去是沒有的功能的console.log = 5的console.log()

//顯然不是指一個功能更多的重新啓動您的環境。即。刷新頁面