Javascript麻瓜: 便利商店買東西,零錢夠不夠 For Javascript Muggle: Are your changes enough for buying a good in convenience store (exercise and codes sample) Please apply what you have learned from the following pages to check if you have enough changes in your pocket to buy a good in a convenience store. https://www.learn-js.org/en/Object_Oriented_JavaScript https://www.learn-js.org/en/Function_Context https://www.learn-js.org/en/Inheritance name of good cost to buy pennies ($0.01) nickels ($0.05) dimes ($0.1) quarters ($0.25) a $3 6 25 30 10 b $10 0 24 15 3 c $25 3 85 1 150 d $15 20 0 250 48 e $8 35 37 1 5 Your codes should be able to print the following output to the console: Good name: a, Cost: 3, Buy? YES Good name: b, Cost: 10, Buy? NO Good name: c, Cost: 25, Buy? YES Good name: d, Cost: 15, Buy? YES Good name: e, Cost: 8, Buy? NO Thefollowing sample codes has applied "...
發表文章
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
Javascript麻瓜:callback 是什麼? For Javascript Muggle: What is callback Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name ‘call back’. More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions. Functions that do this are called higher-order functions . Any function that is passed as an argument is called a callback function . Example 1, function sayHello() { console.log('Hello everyone'); } setTimeout(sayHello(), 3000) function sayHello() is a function and it is passed as an argument of the function setTimeout, i.e. the function setTimeout takes the fucntion sayHello() as its argument. setTimeout is called a higher-order function. sayHello() is called a callback function. The callback function sayHello() is executed after 3000 miliseconds is past. Callbacks ...
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
Javascript麻瓜的練習:Parse URL Exercise for Javascript Muggle: Parse URL Create a function called parse_URL to parse the URL: https://example.com:4000/folder/page.html?x=y&a=b#section-2 //please refer the link to complete the exercise: https://developer.mozilla.org/zh-TW/docs/Web/API/URL/URL The function should print the following text to the console: source: https://example.com:4000/folder/page.html?x=y&a=b#section-2 protocol: https host: example.com port: 4000 query:x=y,a=b path: /folder/page.html hash: section-2 segments: [ '', 'folder', 'page.html' ] Sample codes ============================================================================= function parse_URL(myURL) { var a = new URL(myURL); urlObj= { source: myURL, host: a.hostname, ...