-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpadEnd.js
51 lines (43 loc) · 1.89 KB
/
padEnd.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
48
49
50
51
"use strict";
// TOPIC /////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Array Method: .padEnd()
//
//
// SYNTAX ////////////////////////////////////////////////////////////////////////////////////////////////////
//
// string.padEnd(length-to-pad, padString)
//
// SUMMARY ///////////////////////////////////////////////////////////////////////////////////////////////////
//
// • .padEnd will pad the current string AT THE END with another string as many times as needed until
// the string reaches a given length
// • When you use .padEnd(), you specify the string you want to pad, add the ".padEnd" method, and then
// first specify how many spaces you want to pad and then second what you what string you want to pad with.
//
// EXAMPLES //////////////////////////////////////////////////////////////////////////////////////////////////
//
// EXAMPLE 1: Pad a given number at the end:
// EXAMPLE 2: Hide all but the first 4 digits of a credit card number.
//
// RESOURCES /////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXAMPLE 1: Pad a given number at the end:
const myNum = "5";
function padNum(input) {
const padIt = input.padEnd(9, "*");
console.log(padIt);
}
padNum(myNum);
// EXAMPLE 2: Hide all but the first 4 digits of a credit card number:
const fullNumber = "2034399002125581";
function creditCardNumberHider(num) {
const last4Digits = num.slice(-4); // slice last 4 digits from fullNumber
const maskedNumber = last4Digits.padEnd(num.length, "*"); // replace the rest of full number with asterisks
console.log(maskedNumber);
}
console.log(fullNumber); // 2034399002125581
creditCardNumberHider(fullNumber); // ************5581