/
head.js
71 lines (53 loc) · 1.79 KB
/
head.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
//Week 01, Day 03 and W01, Day 02
//Implement head
//Lotide Challenge with Refactoring
//Anil Patel
//const assertEqual = require('../assertEqual'); //Do not need this
//FUNCTION: Tail
function head(array) {
//const number = array[0]; //return the element itself, not as array
return array[0]
}
//module.exports = assertEqual; // don't export
module.exports = head; //export code from this module //CHECK
//const assertEqual = function(actual, expected) { ... };
//DEPRECATED CODE W01 Day 03
/* INSTRUCTIONS:
https://lodash.com/docs/4.17.15#head
Create a function head which returns the first item in the array.
The head function should not return the first element as an array.
It should simply return the element itself.
Use assertEqual to write test cases for various scenarios.
You should always be thinking about other scenarios to consider. Here are some such examples and how they should be handled:
An array with only one element should still yield that one element as its head
An empty array should yield undefined as its head
*/
//These two could work
/*
function head(array) {
const number = array[0]; //return the element itself, not as array
return number;
}
const tail = function(array) {
const newArray = array.slice(1);
return newArray; //slice good for remove 1st few elements of array
};
*/
/*
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`Assertion Passed ❤: ${actual} === ${expected}`)
return true;
} else { //else runs if is false
console.log(`Assertion Failed :no_entry:: ${actual} !== ${expected}`)
return false;
}
};
*/
/*
// TEST CODE
assertEqual(head([5,6,7]), 5);
assertEqual(head(["Hello", "Lighthouse", "Labs"]), "Hello");
assertEqual("Bootcampp", "Bootcamp");
assertEqual(6, 6);
*/