-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathName_Billboard.js
More file actions
35 lines (24 loc) · 1.25 KB
/
Name_Billboard.js
File metadata and controls
35 lines (24 loc) · 1.25 KB
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
/* ----------------------------------------------------------------------------------------------
You can print your name on a billboard ad. Find out how much it will cost you. Each
character has a default price of £30, but that can be different if you are given 2
parameters instead of 1.
You CAN'T use multiplier "*" operator.
If your name would be Jeong-Ho Aristotelis, ad would cost £600.
20 leters * 30 = 600 (Space counts as a character).
---------------------------------------------------------------------------------------------- */
function billboard(name, price = 30) {
let finalPrice = 0;
for (let i = 0; i < name.length; i++) finalPrice += price;
return finalPrice;
}
/*
Alternative solution:
String.prototype.split(): creates an array of substrings of the given string (one element for
every character in this case)
Array.prototype.reduce(): executes a user-supplied "reducer" callback function on each element
of the array, in order, passing in the return value from the
calculation on the preceding element
*/
function billboard(name, price = 30) {
return name.split("").reduce((sum, letter) => sum + price, 0);
}