-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
146 lines (121 loc) · 2.93 KB
/
test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// console.log('Hello world');
// for(let i=10;i>=0;i--){
// if(i === 0){
// console.log('blast off');
// }
// else{
// console.log(i);
// }
// }
const greeting = 'Hello, world!';
function sendGreeting(name) {
let greeting;
if(name) {
greeting = `Hello, ${name}!`;
return greeting;
} else {
return greeting;
};
};
console.log(sendGreeting());
// Inventory Question:
const Inventory = [
{
item: "scented candle",
stock: 48
},
{
item: "oil diffuser",
stock: 80
},
{
title: "tea kettle",
stock: 16
}
]
console.log(Inventory.length);
let allNames = [];
Inventory.forEach((element)=>{
allNames.push(element.stock);
});
console.log(allNames);
console.log(allNames[0]);
const lowStock = [];
for(let i=0;i<Inventory.length;i++){
if(Inventory[i].stock <50){
lowStock.push(Inventory[i]);
}
}
console.log(lowStock);
// expected result: 4
const uniqueLength = (array) =>{
if(array.length === 0){
return 0;
}
let uniqueNumbers = [];
uniqueNumbers.push(array[0]);
// uniqueNumbers.push(array[0])
for(let i=1;i<array.length;i++){
if(array[i] in uniqueNumbers){
}
else{
uniqueNumbers.push(array[i]);
}
}
console.log(uniqueNumbers.length);
return uniqueNumbers;
}
console.log('UNIQUE');
function uniqueLength_2(numArray){
if(numArray.length === 0){
return 0;
}
let i=0;
let j=1;
let uniqueList = [];
// uniqueList.push(numArray[i]);
while(j<numArray.length){
if(numArray[i] != numArray[j]){
i++;
numArray[i] = numArray[j];
j++;
}
else{
j++;
}
}
// console.log('Unique: ',uniqueList);
return i+1;
}
const exampleArray = [1,1,2,7,88,4,2,1]
console.log('RESULTS:')
// Should return 5
const result = uniqueLength_2([1,1,2,3,4,5,5]);
console.log(result);
// Should return 1
const result2 = uniqueLength_2([1,1,1,1]);
console.log(result2);
//Should return 5
const result3 = uniqueLength_2([1,1,2,7,88,4,2]);
console.log(result3);//1 2 7 88 4
// console.log(uniqueLength(exampleArray));
function findUniqueNumbers(arr) {
const uniqueNumbers = [];
// Iterate through the input array
for (let i = 0; i < arr.length; i++) {
const currentNumber = arr[i];
// Check if the number is already in the uniqueNumbers array
if (uniqueNumbers.indexOf(currentNumber) === -1) {
// If it's not present, add it to the uniqueNumbers array
uniqueNumbers.push(currentNumber);
}
}
return uniqueNumbers;
}
// Test the function
const inputArray = [1, 2, 2, 3, 2, 5, 2, 6, 6];
const uniqueNumbers = findUniqueNumbers(inputArray);
console.log('ANOTHER UNIQUE: ',uniqueNumbers); // Output: [1, 2, 3, 5, 6]
console.log(uniqueNumbers.length);
let testArray = []
console.log(testArray.indexOf())