JavaScript : FUNCTIONS : Return

FUNCTIONS

Return

Menggunakan console.log() sebagai hasil dari sebuah fungsi bukanlah tidak baik digunakan dalam fungsi. Tujuan suatu fungsi adalah untuk mengambil beberapa nilai input, Menjalankan beberapa tugas pada input tersebut dan mengembalikan sebuah nilai.
Untuk mengembalikan nilai, kita bisa menggunakan keyword return. Perhatikan pada Take a look at our function from the last exercise, now re-written slightly:
const getAverage = (numberOne, numberTwo) => { const average = (numberOne + numberTwo) / 2; return average; } console.log(getAverage(365, 27)); // Output: 196
1. Daripada menggunakan  console.log() didalam fungsi the getAverage() , kita menggunakn kata kunci return . return akan mengambil dari  variable dan mengembalikan nilai tersebut.

2. Pada baris terakhir kita memanggil fungsi getAverage() didalam pernyataan console.log() , yang menghasilkan output 196.

3. Code ini menghasilkan output yang sama dengan yang sebelumnya, tapi sekarang kode kita lebih bagus. Kenapa? Jika kita ingin menggunakan kembali fungsi getAverage()  di tempat lain dalam program kita, kita dapat menggunakannya tanpa mencetak hasilnya ke konsol. Penggunaan return  secara umum adalah praktik terbaik ketika menulis fungsi , karena membuat kode anda mudah dipelihara dan fleksibel.
1.
Now that we have the pizza orders, you want to add them up to find the cost of the pizzas for the check. Let's imagine that each pizza is $7.50, no matter the topping and crust type.
We will need to do three things to write this in JavaScript:
  • Create a variable to hold the number of pizzas ordered.
  • Whenever a pizza is ordered, add one to the number of pizzas ordered.
  • Take the total number of pizzas and multiply them by 7.5, since each pizza is $7.50.
Begin by creating a variable named orderCountset equal to 0 at the top of your code.


Instructions
1.
Sekarang kita memiliki pesanan pizza, Anda ingin menambahkannya untuk menemukan biaya pizza untuk cek. Mari kita bayangkan bahwa setiap pizza adalah $ 7,50, tidak peduli jenis topping dan kerak.
Kami perlu melakukan tiga hal untuk menulis ini di JavaScript:
Buat variabel untuk menampung jumlah pizza yang dipesan.
Setiap kali pizza dipesan, tambahkan satu ke jumlah pizza yang dipesan.
Ambil jumlah pizza total dan kalikan dengan 7,5, karena setiap pizza adalah $ 7,50.
Mulai degnan membuat sebuah varibel bernama orderCount set sama degnan 0 pada bagian atas code anda.
Hint
Make sure to use let to declare a variable to define orderCount above the takeOrder() function. We will increment the orderCount variable in the function in the next step.
2.
Inside the takeOrder()function, add 1 to the value of orderCount each time takeOrder() is called.
Hint
You can increment the orderCount by +1 using either of the following:
orderCount = orderCount + 1;
orderCount += 1
The shorthand notation for this is:
orderCount++;
3.
Now it's time to calculate the subtotal of the pizzas. This is the perfect job for a function.
On a new line, beneath the closing brackets of the takeOrder function, declare a new function named getSubTotal that has one parameter named itemCount.
Stuck? Get a hint
4.
Inside the getSubTotal()function's block, use return to output the itemCountmultiplied by 7.5.
Stuck? Get a hint
5.
On the last line of your program, after the takeOrder()function calls, call the getSubTotal()function inside a console.logstatement.
getSubTotal has a parameter that represents the number of items ordered. Pass in the orderCount as an argument when making the function call.
Nice work! Now you can see the orders taken and how much it costs.

let orderCount = 0;

const takeOrder = (topping, crustType) => {
  orderCount++;
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');

const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};

console.log(getSubTotal(orderCount));

Output :

Order: thin crust pizza topped with mushroom
Order: whole wheat pizza topped with spinach
Order: brooklyn style pizza topped with pepperoni
22.5

Tidak ada komentar:

Posting Komentar