-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavo.js
47 lines (39 loc) · 1.17 KB
/
avo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var deal = "1 for R3, 2 for R7, 3 for R10, 5 for R14.50";
function splitString(stringToSplit, separator){
var arrayOfStrings = stringToSplit.split(separator);
return arrayOfStrings;
}
var x = splitString(deal, ',')
//Removing 'for R' from each
var a = [];
for (var i =0; i < x.length; i++){
var n = x[i].split(' for R')
a.push(n);
}
//Pushing prices for each deal to an array
var prices =[];
a.forEach(function(item){
var qua = item[0];
var price = item[1];
var ans = Number((price/ qua).toFixed(2));
prices.push(ans)
})
// Find out about Accessing the last item in the list
var sorted = prices.sort();
console.log("\n---------------------");
console.log(sorted)
console.log("---------------------\n");
console.log("----------------");
console.log("Cheap : " + sorted[0]) ;
console.log("Expensive : " + sorted[sorted.length - 1])
console.log("----------------\n");
//Sum all numbers in the array
var sum = 0;
sorted.forEach(function(item){
sum += item;
})
//Get the average of the prices
var average = sum / prices.length;
console.log("----------------------------");
console.log("The average price is :" + average.toFixed(2) + " |");
console.log("----------------------------\n");