Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

Wrong result of JavaScript function (math operations)

Can someone please breakdown this script for me and tell me how the outcome is 30105? Thanks alot guys

function one(numOneIn) {

two(numOneIn * 3); //1x3

document.write (numOneIn); //3

}

function two(aNumberIn) { 

document.write (aNumberIn); //1

}

function three(numIn) {

one(numIn * 2); 

document.write (numIn);

}

three(5);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

three(5)

calls three, passing the number 5, then:

one(numIn * 2);

calls one, passing 5 x 2 => 10, then:

two(numOneIn * 3)

calls two, passing 10 x 3 => 30, then

two writes "30" as a string to the document.

Control returns to one, which writes the value passed to it (10) as a string.

Control returns to three, which writes the value passed to it (5) as a string.

So the final string is "30105".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...