-
Notifications
You must be signed in to change notification settings - Fork 45
solutions #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
solutions #91
Changes from all commits
0da7e56
12c59d7
f7cf959
4578cee
0c09110
b1527a2
135eee5
c0fb404
64eed4c
a5fc416
76c3bb4
2820848
778e1b6
c594601
7c7010b
190f7e0
33b2a33
35aa5f4
52ac133
46d5114
597dbdd
889b320
48c7a38
7879ef2
ccb16f3
56c4f29
da34150
b4eb306
9a5fab9
9f9dbd5
a7dfae4
a2487c0
8ab6104
f9f77e2
a726987
bd00968
c22bc29
ee5e840
adaca0c
4d1870b
4ca9d5c
97ed2df
7f79177
35f43cb
f305a40
3aa94a4
8ad33bd
177465e
196611b
12712c9
a42ab54
1f27ffe
29ab0f1
751fd98
673b0d5
ccfa367
825839e
2be3575
f2cea87
d33a6b9
370d169
87e626c
afc439f
e0c167a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,39 @@ | |
| * How do these operations compare to that of a linked list? | ||
| * How does the time complexity of insertion and deletion compare to that of a linked list? | ||
| */ | ||
|
|
||
| class Array { | ||
| constructor() { | ||
| this.array = {}; | ||
| this.key = 0; | ||
| } | ||
| push(value) { | ||
| this.array[this.key] = value; | ||
| this.key += 1 | ||
| } | ||
| pop() { | ||
| const value = this.array[this.key - 1]; | ||
| delete this.array[this.key - 1]; | ||
| this.key -= 1; | ||
| return value; | ||
| } | ||
| get(index) { | ||
| //console.log(index); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented out code. |
||
| return this.array[index]; | ||
| } | ||
| delete(index) { | ||
| delete this.array[index]; | ||
| for (let i = index; i < this.key; i++) { | ||
| this.array[i] = this.array[i + 1]; | ||
| } | ||
| this.key--; | ||
| } | ||
| } | ||
|
|
||
| const a = new Array(); | ||
| a.push(5); | ||
| a.push(53); | ||
| a.push(54); | ||
| console.log(a); | ||
| a.delete(0); | ||
| console.log(a); | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). | ||
| How would you come up with exactly four quarts of water? | ||
| Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible. | ||
|
|
||
| i would fill both the 3 and 5 quart jugs at the same time and stop the water flow when the 3 | ||
| quart jug is full. Now the 5 quart jug has 3 quarts and the 3 quart jug is filled. Then, i will pour | ||
| out the 3 quart jug in the 5 quart jug until it is filled. Now my 3 quart jug is left with 1 quart. Now | ||
| i empty the 5 quart jug and pour the 1 quart from the 3 quart jug. Finally, i will fill the 3 quart jug | ||
| and then pour it in the 5 quart jug that currently has one quart and i will end up with 4 quarts in the 5 quart jug. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,52 +19,79 @@ | |
| // Write a function called firstItem that passes the first item of the given array to the callback function | ||
|
|
||
| const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango']; | ||
| const firstItem = (arr, cb) => { | ||
| return cb(arr[0]); | ||
| }; | ||
|
|
||
| firstItem(foods, (firstItem) => { | ||
| console.log(`The first item is ${firstItem}.`); | ||
| }); | ||
|
|
||
| // Write a function called getLength that passes the length of the array into the callback | ||
| const getLength = (arr, cb) => { | ||
| return cb(arr.length); | ||
| }; | ||
|
|
||
| getLength(foods, (length) => { | ||
| console.log(`The length of the array is ${length}.`); | ||
| }); | ||
|
|
||
| // Write a function called last which passes the last item of the array into the callback | ||
|
|
||
| const last = (arr, cb) => { | ||
| cb(arr[arr.length - 1]); | ||
| }; | ||
| last(foods, (lastItem) => { | ||
| console.log(`The last item in the array is ${lastItem}.`); | ||
| }); | ||
|
|
||
| // Write a function called sumNums that adds two numbers and passes the result to the callback | ||
|
|
||
|
|
||
| const sumNums = (x, y, cb) => { | ||
| cb(x + y); | ||
| }; | ||
| sumNums(5, 10, (sum) => { | ||
| console.log(`The sum is ${sum}.`); | ||
| }); | ||
|
|
||
| // Write a function called multiplyNums that adds two numbers and passes the result to the callback | ||
|
|
||
| const multiplyNums = (x, y, cb) => { | ||
| return cb(x * y); | ||
| }; | ||
| multiplyNums(5, 10, (product) => { | ||
| console.log(`The product is ${product}.`); | ||
| }); | ||
|
|
||
| // Write a function called contains that checks if an item is present inside of the given array. | ||
| // Pass true to the callback if it is, otherwise pass false | ||
|
|
||
| const contains = (arr, item, cb) => { | ||
| return cb(arr.includes(item)); | ||
| }; | ||
| contains(foods, 'ribeye', (result) => { | ||
| console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array'); | ||
| }); | ||
|
|
||
| // Write a function called removeDuplicates that removes all duplicate values from the given array. | ||
| // Pass the array to the callback function. Do not mutate the original array. | ||
|
|
||
| // const removeDuplicates = (arr, cb) => { | ||
| // const set = new Set(arr); | ||
| // console.log(set); | ||
| // return cb(set); | ||
| // }; | ||
| const removeDuplicates = (arr, cb) => { | ||
| const newArray = arr.filter((item, index, inputArray) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of Filter here! It's one of my favorite JS methods. Any chance you could try and solve this without using it? |
||
| return inputArray.indexOf(item) === index; | ||
| }); | ||
| cb(newArray); | ||
| }; | ||
| removeDuplicates(foods, (uniqueFoods) => { | ||
| console.log(`foods with duplicates removed: ${uniqueFoods}`); | ||
| }); | ||
|
|
||
| // Write a function called forEach that iterates over the provided array and passes the value and index into the callback. | ||
|
|
||
| const forEach = (arr, cb) => { | ||
| for (let i = 0; i < arr.length; i++) { | ||
| cb(arr[i], i); | ||
| } | ||
| }; | ||
| forEach(foods, (value, index) => { | ||
| console.log(`${value} is at index ${index}.`); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,33 @@ | ||
| /** | ||
| * Common Characters: | ||
| * Write a function that accepts two strings as arguments, and returns only the characters that are common to both strings. * | ||
| * Your function should return the common characters in the same order that they appear in the first argument. | ||
| * Do not return duplicate characters and ignore whitespace in your returned string. * | ||
| * Example: commonCharacters('acexivou', 'aegihobu') * | ||
| * Common Characters: | ||
| * Write a function that accepts two strings as arguments, and returns only the characters that are common to both strings. * | ||
| * Your function should return the common characters in the same order that they appear in the first argument. | ||
| * Do not return duplicate characters and ignore whitespace in your returned string. * | ||
| * Example: commonCharacters('acexivou', 'aegihobu') * | ||
| * Returns: 'aeiou' | ||
| */ | ||
|
|
||
| const eliminateWhitespace = (str) => { | ||
| const re = /\s/g; | ||
| str = str.replace(re, ''); | ||
| return str; | ||
| }; | ||
|
|
||
| const commonCharacters = (str1, str2) => { | ||
| const uniqueCharacters = new Set(); | ||
| str1 = eliminateWhitespace(str1); | ||
| str2 = eliminateWhitespace(str2); | ||
| str1 = str1.split(''); | ||
|
|
||
| for (let i = 0; i < str1.length; i++) { | ||
| if (str2.includes(str1[i])) { | ||
| uniqueCharacters.add(str1[i]); | ||
| } | ||
| } | ||
| const commonLetters = Array.from(uniqueCharacters).join(''); | ||
| return commonLetters; | ||
| }; | ||
|
|
||
| let str1 = 'acex ivou'; | ||
| const str2 = 'aegihobu'; | ||
| console.log(commonCharacters(str1, str2)); // --> 'aeiou' |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should indent these lines inside of a function block.