get help in general - we have more specialized help rooms here: https://gitter.im/FreeCodeCamp/home
I need to check if the bar is in the database. If the bar is not in the database, then I need to put it in
maybe using a method like findOneAndUpdate()
with upsert: true
would handle that, along with the flag to return the modified (or new, if upsert) document, which you could then check on the front end to see if your user is present?
But if the bar is in the database, I need to check to see if the user had already clicked "going" by seeing if the user is $in the userid array for the bar.
If you just grab all the bars in the database using @thekholm80 suggestion, you can use the results to determine if they have already clicked 'going'
sgoldber61 sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1366 | @cmccormack |http://www.freecodecamp.org/cmccormack
sgoldber61 sends brownie points to @thekholm80 :sparkles: :thumbsup: :sparkles:
:star2: 1713 | @thekholm80 |http://www.freecodecamp.org/thekholm80
p
element usually looks like this - <p>This is my paragraph</p>
smaller-image
Create a class called smaller-image
and use it to resize the image so that it's only 100 pixels wide.
function mySort(nums) {
// empty array to hold only integer elements
const num = [];
// get rid of strings and round nums
for (let i = 0; i < nums.length; i++)
{
// if a number, push to n
if (!isNaN(nums[i]))
{
Math.trunc(num.push[i]);
}
return num;
}
}
const array = [ 90, 45, 66, 'bye', 100.5 ];
console.log(mySort(array));
num.push[i]
it should be num.push(nums[i])
. Second you'd want to return num
after the loop is over cause rn it returns num
after first iteration.
Math.trunc
since it returns the truncated value so basically you'd want to do this num.push(Math.trunc(nums[i]));
instead
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
should return [1, 3, 5, 7, 9, 2, 4, 6, 8]
.sort(function (a,b) {})
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let arr1 = [];
let arr2 = [];
arr.forEach(x=>x%2 !==0?arr1.push(x):arr2.push(x))
arr1.concat(arr2);
binary and
operation
a & 1 === 1 ? 'odd' : 'even'
a
is a number
if else
, so it checks elements if its odd it pushes to first array, and if its even it pushes to 2nd array and then concat
concatenates them.arr.forEach(function(x){
if (x%2 !==0){
arr1.push(x)
}
else {
arr2.push(x)
}
})
function mySort(nums) {
return nums
.filter(n => !isNaN(n))
.map(n => Math.round(n))
.sort((a,b) => a-b)
}
const array = [ 90, 45, 66, 'bye', 100.5 ];
const output = mySort(array);
const odds = output.filter(n => n&1);
const evens = output.filter(n => n&1 ? false: true);
console.log(output); // [ 45, 66, 90, 101 ]
console.log(odds); // [ 45, 101 ]
console.log(evens); // [ 66, 90 ]
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr.sort((a, b)=> b % 2 - a % 2 || a - b) //[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
function mySort(nums) {
const num = [];
// get rid of strings and round nums
for (let i = 0; i < nums.length; i++)
{
// if a number, push to n
if (!isNaN(nums[i]))
{
num.push(Math.trunc(nums[i]));
}
}
let odd = [];
let even = [];
// loop through num and push accordinly to the appropriate array
num.forEach( function (x) {
x % 2 !== 0 ? odd.push(x) : even.push(x);
});
// return both arrays added
return odd.concat(even);
}
num.forEach
.push
and .sort
.concat
too
.push()
?
[ 90, 45, 66, 'bye', 100.5 ];
[ 45, 90, 66, 100 ]
[ 45, 66, 90, 100 ]
intergration - higher order function
derivative - lower level, pure function...
combined together it's functional math - thus the name, functional programming...
.push
higher order
.concat
higher order
.sort
higher order
@ezioda004
This part
num.forEach( function (x) {
x % 2 !== 0 ? odd.push(x) : even.push(x);
});
can be implemented in pure fashion as
var oddCount = 0, evenCount = 0;
for(var i=0; i<num.length; num++){
if(x%2 !== 0){
odd[oddCount++] = num[i]
} else {
even[evenCount++] = num[i];
}
}
Ah yes but this so much work >.<
@Frediflexta This should work
function mySort(nums) {
const num = [];
// get rid of strings and round nums
for (let i = 0; i < nums.length; i++)
{
// if a number, push to n
if (!isNaN(nums[i]))
{
num.push(Math.trunc(nums[i]));
}
}
num.sort((a, b)=>a-b)
let odd = [];
let even = [];
// loop through num and push accordinly to the appropriate array
num.forEach( function (x) {
x % 2 !== 0 ? odd.push(x) : even.push(x);
});
// return both arrays added
return odd.concat(even);
}
@ezioda004
Pros:
1) Faster
2) You understand how it works. (purely for the sake of learning)
3) Lesser abstraction
Cons:
1) Longer code
.sort
method?
frediflexta sends brownie points to @ezioda004 and @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:cookie: 341 | @sweetcodinginc |http://www.freecodecamp.org/sweetcodinginc
:cookie: 498 | @ezioda004 |http://www.freecodecamp.org/ezioda004
sort
reduce
map
etc?
@SweetCodingInc I see, so would you say that these algorithms/native ways are used in production? Cause I have no idea.
Also, can you sort an array without using native
.sort
method?
I think I can I've wrote a sorting algorithm for a challenge a long time ago, it was very big lol
@ezioda004
I see, so would you say that these algorithms/native ways are used in production? Cause I have no idea.
Dev and prod are completely different things. We're talking in terms of learning and becoming a badass programmer.
.sort()
since its faster? Or is the speed difference is not that much so why bother kinda thing?
ezioda004 sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:cookie: 342 | @sweetcodinginc |http://www.freecodecamp.org/sweetcodinginc
best
is a relative term
how do I add an element to array without using .push
, may be it's time to master what you do know so far
mukeshangrish sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:cookie: 343 | @sweetcodinginc |http://www.freecodecamp.org/sweetcodinginc
ezioda004 sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
~~
, so I have that going for me :)
~
in the past for making nicer indexOf
lookups but never used it to coerce to an integer
String(float).split('.')[0]
kind of guy?
@kbaig
The bitwise operator is a bit diff but in almost all cases does the same thing as floor
and a hell of a lot faster
props.onUpdateRecipe
function, which updates your component and resets the state
this.props.onUpdateRecipe
on click of save button
this.props.onUpdateRecipe
on click of that
handleNameUpdate
state
change to false then?
handleNameUpdate
and inside it, I can change the edit
and call the this.props.onUpdateRecipe
. But the only problem here is I need to pass either the input
element's text or target value
{this.state.edit && (
<input
ref={(input) => { this.recipeTitle = input; }}
className = "recipe-name-input"
onChange = { this.handleNameUpdate }
value = { this.props.name } />
)}
{this.state.edit && (
<i
className = "fas fa-check edit-name"
id = { "edit-name-" + this.props.index }
onClick = { () => {
this.setState({ edit : false });
this.props.onUpdateRecipe(this.props.index, this.recipeTitle.value, this.props.ingredients);
}}>
</i>
)}
debounce
the update. Means any time an input comes in, you trigger a future update of the state - like 500ms after the change. If another input change comes in until then, you cancel the last timeout (delay) and create a new one. That way the state will only be set after you are done with writing or make a pause. You could also put it in a temporary state when a change comes in and only put it into the right state when the focus on the input is lost