-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.js
59 lines (47 loc) · 1.06 KB
/
05.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
/**
* Check if string is a palindrome with string manipulation
*/
const string = 'hannah'
function isPalindromeStr(myStr) {
const palindrome = myStr.split('').reverse().join('')
return myStr === palindrome
}
console.log(isPalindromeStr(string))
/**
* Compare if two arrays are the name
* @type {Array}
*/
var myArr = ["foo", "bar", "baz"];
var myArr2 = ["foo", "bar", "baz"];
function compareArray(array1, array2) {
if (array1.length !== array2.length) {
console.log('Arrays are not the same length')
return false
}
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false
}
}
return true
}
console.log(compareArray(myArr, myArr2))
/**
* Compare if a string is a palindrome by checking the value of each index
*/
const myStr = 'racecarzz'
function algoPali(str) {
var i = 0
var j = str.length -1 //arrays are 0 based index
while(i < j) {
if (str.charAt(i) !== str.charAt(j)) {
return false
}
else {
i++
j--
}
}
return true
}
console.log(algoPali(myStr))