forked from ManspergerMichael/Algorithims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrays.js
45 lines (43 loc) · 1.13 KB
/
Arrays.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
//replace each positive number with "Embiggens"
function makeBig(arr){
if(arr.length <=0){
return "Error"
}
for(var i = 0; i<arr.length;i++){
if(arr[i] > 0){
arr[i] = "Embiggens";
}
}
return arr;
}
//console.log(makeBig([-1,2,-3,4,-5]));
//replace each string in a array with the length of the string.
function lengths(arr){
for(var i = 0; i<arr.length; i++){
arr[i] = arr[i].length;
}
return arr;
}
var strArr = ['hello','my','giant','blue','cat'];
//console.log(lengths(strArr));
//take an array and print the sum of the length of
//the first element of the array and the length of the array
function firstPlusLength(arr){
var firstElementVal;
if(typeof arr[0] === 'string'){
firstElementVal = arr[0].length;
}
if(typeof arr[0] === 'boolean'){
if(arr[0] == false){
firstElementVal = 0;
}
else{
firstElementVal = 1;
}
}
if(typeof arr[0] === 'number'){
firstElementVal = arr[0];
}
return firstElementVal + arr.length;
}
//console.log(firstPlusLength(["Titlywinks",2,3,4]));