-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirmEnding.js
72 lines (47 loc) · 1.77 KB
/
confirmEnding.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Check if a string (first argument, str) ends with the given target string (second argument, target).
*/
function confirmEnding(str, target) {
let fragment = str.slice(str.length - target.length);
//console.log(fragment);
if (target == fragment){
return true;
}
else {
return false;
}
}
/////// MORE CONCISE:
function confirmEnding(str, target) {
if (target == str.slice(str.length - target.length)){
return true;
}
else {
return false;
}
}
///////// TERNARY OPERATOR:
function confirmEnding(str, target) {
return (target == str.slice(str.length - target.length)) ?
true : false
}
////// In JUST ONE LINE:
function confirmEnding(str, target) {
return str.slice(str.length - target.length) == target;
}
//// EVEN SHORTER
// If a negative number is provided as the first parameter to slice() , the offset is taken backwards from the end of the string
function confirmEnding(str, target) {
return str.slice(-target.length) === target
}
// TESTS:
console.log(confirmEnding("Bastian", "n")); // true
console.log(confirmEnding("Congratulation", "on")); // true
console.log(confirmEnding("Connor", "n")); // false
console.log(confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")); // false
console.log(confirmEnding("He has to give me a new name", "name")); // true
console.log(confirmEnding("Open sesame", "same")); // true
console.log(confirmEnding("Open sesame", "sage")); // false
console.log(confirmEnding("Open sesame", "game")); // false
console.log(confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")); // false
console.log(confirmEnding("Abstraction", "action")); //true