Thursday, November 18, 2021
The forEach() method loops through an array, and performs a function on each element of the array. The forEach() method can take three arguments, the first argument is a current value that you want to perform an action on. The second argument acts as the "this" value within the scope of the function. The third argument is the array itself. Most of the time only the first argument is passed. The code below passes in only one argument which is an anonymous function being performed on each element of the array using the forEach() method to calculate the oil prices in the last five days and prints it out in the console.
You should get the total of 362.19 as your total when you add up the last five days of oil prices.
If you want to pass in all three values you can have a function that can manipulate the actual value itself like the code below. Let's say you buy your oil in large quantity so you get a $1 discount for each barrel of oil. The following forEach() method will show you the discounted oil prices.
As you can you got yourself a discount
<script> var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99]; var total = 0; oilPrices.forEach(function (value) { total += value }); console.log(total); </script>
You should get the total of 362.19 as your total when you add up the last five days of oil prices.
If you want to pass in all three values you can have a function that can manipulate the actual value itself like the code below. Let's say you buy your oil in large quantity so you get a $1 discount for each barrel of oil. The following forEach() method will show you the discounted oil prices.
<script> var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99]; var total = 0; oilPrices.forEach(function (value) { total += value }); console.log(total); oilPrices.forEach(function (value, index, prices) { prices[index] = value - 1; }); console.log(oilPrices); </script>
As you can you got yourself a discount
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
No comments:
Post a Comment