舉例輸入
2.6
舉例輸出
21.237166338267002
16.336281798666924
解析
這題連hackerRank都懶得把方法提出去了
這邊簡單說一下let和const吧
他的生命周期只在一個{}之內
因此出了這個{}就叫不到他,但{}之內的{}則可以呼叫到
用後端java的話叫做final 是一個不可變的常數
但對JS來說只是這塊記憶體位置的內容不可再變動
啥意思?
const o = {}
o.name="itHome"
console.log(o);//{name: "itHome"}
const a = []
a[0]="array";
console.log(a)//["array"]
a="3";//Assignment to constant variable.
const i = 1;
i=2;//Assignment to constant variable.
從上面可以知道JS只是存放了參考位址,但參考位址所指向的位址內容是可以變動的
但若直接更改參考位址內的值則會直接報錯
好的,那麼今天的答案
function main() {
let r = readLine();
const PI = Math.PI;
console.log(r * r * PI);
console.log(2 * r * PI);
try {
// Attempt to redefine the value of constant variable PI
PI = 0;
// Attempt to print the value of PI
console.log(PI);
} catch(error) {
console.error("You correctly declared 'PI' as a constant.");
}
}
本日心得
ES6之前的var實在太自由了,搞到最後如果命名沒命好真是不知道呼叫了甚麼
如有需訂正的地方歡迎告知,若有更好的解法也歡迎留言,謝謝。