Fat Arrows and This

Alrighty, without giving me the answer, let me know A) this question makes sense to you and B) if this is something I should care about. I'm stumped.
No description
154 Replies
Rägnar O'ock
Rägnar O'ock2mo ago
fat-arrow? who calls them that ?
CDL
CDLOP2mo ago
this course apparently, threw me off too oh, apparently the solution is to change it from an arrow function back to a regular function wtf is the point
Rägnar O'ock
Rägnar O'ock2mo ago
showing that function expressions bind this and not arrow functions
CDL
CDLOP2mo ago
gotcha. jeez being old is rough
ἔρως
ἔρως2mo ago
everybody
Rägnar O'ock
Rägnar O'ock2mo ago
I odn't believe you
vince
vince2mo ago
Isn't the nomenclature 'anonymous functions'?
Rägnar O'ock
Rägnar O'ock2mo ago
No this is an anonymous function:
export default function () { console.log('hi!'); }
export default function () { console.log('hi!'); }
vince
vince2mo ago
oooo
Rägnar O'ock
Rägnar O'ock2mo ago
ECMAScript® 2026 Language Specification
Introduction This Ecma Standard defines the ECMAScript 2026 Language. It is the seventeenth edition of the ECMAScript Language Specification. Since publication of the first edition in 1997, ECMAScript has grown to be one of the world's most widely used general-purpose programming languages. It
ἔρως
ἔρως2mo ago
anonynous function expression
Rägnar O'ock
Rägnar O'ock2mo ago
Well yes... But it defines an anonymous function so... I'm also correct
ἔρως
ἔρως2mo ago
it is and it is very correct
vince
vince2mo ago
function anonymous() {}
function anonymous() {}
This is an anonymous function ha ha ha
ἔρως
ἔρως2mo ago
no, that's a function called anonymous
CDL
CDLOP2mo ago
No description
CDL
CDLOP2mo ago
I would never have guessed the correct answer was
class SMSSender extends Sender {
formatMessage(message) {
return `${super.formatMessage(message)} [SMS]`;
}
}
class SMSSender extends Sender {
formatMessage(message) {
return `${super.formatMessage(message)} [SMS]`;
}
}
vince
vince2mo ago
Iirc with no knowledge of OOP I think super refers to the parent object, so it would refer to Sender
CDL
CDLOP2mo ago
Yeah I get it now, I completely missed that underneath the initial bit of code, there was
formatMessage(message) {
return `To: ${this.recipient}, Message: ${message}`;
}
formatMessage(message) {
return `To: ${this.recipient}, Message: ${message}`;
}
vince
vince2mo ago
Yea makes sense, parent class has all the methods and then child class just spits out the parent method return with some additional output attached
CDL
CDLOP2mo ago
so return ${super.formatMessage(message)} [SMS]; is just using return To: ${this.recipient}, Message: ${message}; and uhh.. adding some.
vince
vince2mo ago
yea
CDL
CDLOP2mo ago
makes sense useful, too
vince
vince2mo ago
And then you'd make another one for [Email] too
CDL
CDLOP2mo ago
yeah got that one, phew, classes done (short, hmm)
CDL
CDLOP2mo ago
No description
CDL
CDLOP2mo ago
the fuck is this?
No description
ἔρως
ἔρως2mo ago
that's easy
CDL
CDLOP2mo ago
the floats are throwing me off massively. Especailly when the intro was "hey here's a loop that increments by 1 each time" xD in fact, even the next assignment is seriously messing with me
ἔρως
ἔρως2mo ago
dude, this is super easy: count + (count / 100) - 0.01 oh, wait, no, it's incrementing yeah, that's basically this then: 1 + (index / 100), for each loop but there's a formule to calculate this without a loop https://stackoverflow.com/a/50566815 maybe try the first answer instead
CDL
CDLOP2mo ago
function bulkSendCost(numMessages) {
let totalCost = 0;
for (let i = 0; i < numMessages; i++) {
totalCost += 1.0 + (i * 0.01);
}
return totalCost;
}

export { bulkSendCost };
function bulkSendCost(numMessages) {
let totalCost = 0;
for (let i = 0; i < numMessages; i++) {
totalCost += 1.0 + (i * 0.01);
}
return totalCost;
}

export { bulkSendCost };
I had no idea how to get that totalCost sum though, tbh. I would've never guessed += 1.0 (i * 0.01) maybe it's just a mental block I have or something but the entire section I Struggled with and had so much help to get right
ἔρως
ἔρως2mo ago
this is actually basic math, not a language thing you can do numMessages + (((numMessages - 1) * numMessages) / 200)
CDL
CDLOP2mo ago
💀 wtf
ἔρως
ἔρως2mo ago
which is what's in the link i've sent, but applied to that exercise
CDL
CDLOP2mo ago
I'm either uneducated or the lessons aren't clear, and by the number of people completing them, it's not the lessons
vince
vince2mo ago
I think you could just do something like this too: (thanks Epic for the math I was completely blanking on how to get the 0.01 lmao)
let total = 0;
const BASE_COST = 1.0;

messages.forEach((message, index) => {
total += BASE_COST + (index / 100);
});
let total = 0;
const BASE_COST = 1.0;

messages.forEach((message, index) => {
total += BASE_COST + (index / 100);
});
ἔρως
ἔρως2mo ago
i would try something with reduce that's what it is for
vince
vince2mo ago
Always forget about reduce
ἔρως
ἔρως2mo ago
but that is to loop over the messages, by the way if all you get is the number of messages, your solution doesn't work unless you create and fill an array of elements
vince
vince2mo ago
Ahhh thought there was a messages array
CDL
CDLOP2mo ago
don't worry we're passed that one, I'm now having to check if a number is prime but I have to use continue and break ^^
ἔρως
ἔρως2mo ago
i don't know if there is
vince
vince2mo ago
I think same way would work though
ἔρως
ἔρως2mo ago
if you make an array and fill it, it works
vince
vince2mo ago
You could just do numMessages.forEach((_, index) => {}); but yea reduce is definitely better
ἔρως
ἔρως2mo ago
numMessages would be a number
vince
vince2mo ago
Or is numMessage an int Ohh I'm dumb Hehe
CDL
CDLOP2mo ago
left is my attempt, right is the answer 😄
No description
ἔρως
ἔρως2mo ago
does it specify what should happen to 1?
CDL
CDLOP2mo ago
yeah 1 can't be printed This course doesn't agree with me anymore lol, I can't get my mental around the wording, I'd be better off just doing bloody codewars at this rate
ἔρως
ἔρως2mo ago
that answer is so terrible: it lacks memoization but js doesn't have good options for that it works, but it's so unoptimal
Rägnar O'ock
Rägnar O'ock2mo ago
I oppose you :
function bulkSendCost(numMessages) {
return (function* () {
for(let i = 0;;) {
yield i++;
}
})()
.take(numMessages)
.reduce((acc, i) => acc + i * .01, 0) + numMessages;
}
function bulkSendCost(numMessages) {
return (function* () {
for(let i = 0;;) {
yield i++;
}
})()
.take(numMessages)
.reduce((acc, i) => acc + i * .01, 0) + numMessages;
}
ἔρως
ἔρως2mo ago
forgot those thingies exist now
Rägnar O'ock
Rägnar O'ock2mo ago
there is sadly no clean way to get an infinite iterator :/
CDL
CDLOP2mo ago
What in the Minecraft is that code
Rägnar O'ock
Rägnar O'ock2mo ago
this creates and infinite iterator that simply enumerates positive integers :
function* () {
for(let i = 0;;) {
yield i++;
}
})()
function* () {
for(let i = 0;;) {
yield i++;
}
})()
then we .take() the first numMessages and .reduce() them into the index / 100 then add the constant 1 for each (i.e. add the number of messages) technically, it should be possible to find the canonical form of the mathematical suite and have the result without using any iteration
ἔρως
ἔρως2mo ago
like this, but better optimized that code is a bit shoddy
Rägnar O'ock
Rägnar O'ock2mo ago
iterators are fun
ἔρως
ἔρως2mo ago
they are it is rare for me to use those, but they are cool
Rägnar O'ock
Rägnar O'ock2mo ago
this function would give you the right answer too...
function bulkSendCost(numMessages) {
return numMessages + 0.005 * numMessages * (numMessages - 1);
}
function bulkSendCost(numMessages) {
return numMessages + 0.005 * numMessages * (numMessages - 1);
}
no iteration needed
ἔρως
ἔρως2mo ago
it's basically what i wrote, but less bad
Rägnar O'ock
Rägnar O'ock2mo ago
oh, yeah, had not understood what you meant by what you sent, but yeah math is neat
ἔρως
ἔρως2mo ago
it is really neat i suck at it, but it is neat a while ago, i found an algorithm that lets you calculate a running average imagine you have 5467372 star ratings instead of recalculating it all for the 5467373rd one, you run a formula that just spits out the new average i remember you just need the current count, the next count, the current average and the new value
Ganesh
Ganesh2mo ago
Wouldn't you need to subtract numMessages/100 from the product of this to get the correct result Because the dynamic charge is 0 at first message and begins from second message ah nevermind it works because - instead of plus
CDL
CDLOP2mo ago
None of ^ made any sense 😂
ἔρως
ἔρως2mo ago
the formula to calculate the sum of all numbers from 0 to n requires you to divide by 2 your challenge requires the division by 100, to get 0.00, 0.01, 0.02 ... so, if you divide 1 by 200, you get 0.005 why 200? because you divide by 2 then 100, which is the same as dividing by 200
Ganesh
Ganesh2mo ago
the exact formula for summing all numbers from 0 to n looks like this btw n(n+1)/2 where n is the numbers you want to count up to. for example I want the sum of all numbers from 0 to 3 so 3*(3+1)/2 result is 6 .
ἔρως
ἔρως2mo ago
that is a different version of the formula we used, but it works the same
Ganesh
Ganesh2mo ago
I was giving the basic formula yeah.
CDL
CDLOP2mo ago
Oh, this is like a purely math assignment which is why I failed lol
Ganesh
Ganesh2mo ago
Well you might encounter problems like these on a job, this was a pretty basic problem
ἔρως
ἔρως2mo ago
no, this is the "no loop" solution you can use a loop, as explained before
Rägnar O'ock
Rägnar O'ock2mo ago
The loop solution from before works, but if you had to handle requests for a million messages sent it would take a while because it's O(n). The math solution is O(1), no matter the number of messages to send it'll take the same amount of time to compute. But if all you need is the result and you don't care about perf (like is the case in your assignment) both are perfectly valid.
ἔρως
ἔρως2mo ago
yup, but luckly it only slows down linearly and not exponentially
CDL
CDLOP2mo ago
So is the assignment a common occurrence? If so im gonna need to study this more, specifically.
ἔρως
ἔρως2mo ago
no it's just basic math it isnt common, but it isnt obscure it's just something to make the mind work
Ganesh
Ganesh2mo ago
I think it's another of those googleable thing. I didn't know the formula myself but I did recognise the pattern and just googled formula for summation of a sequence of numbers Then went from there
ἔρως
ἔρως2mo ago
and this simple exercise shows you have some problems with extracting information from exercises same but the problem here is that he couldnt understand what the exercise was about it wasnt super clear, i will admit it, but it isnt complicated
Ganesh
Ganesh2mo ago
Yeah true If you can't understand then you can't google
ἔρως
ἔρως2mo ago
exactly
Rägnar O'ock
Rägnar O'ock2mo ago
Yeah, ya need to work on that CDL, most of what a developer does is turn shitty explanations from the client and turn it into specs before implementing them. It's important to be able to find patterns in the shitty explanations to have simple specs and simple code in the end
ἔρως
ἔρως2mo ago
and this one is an easier one imagine someone who wants a booking system that can light a cigar while baking a pie you have to extract what matters - the booking system
CDL
CDLOP2mo ago
Basic math? 💀 basic math to me is 2*2 That’s pretty much held me back for 3 years lol, I’ve no idea how to improve it, or if I can. Obviously if I can’t then there’s no point continuing.
Rägnar O'ock
Rägnar O'ock2mo ago
That's honestly part of most jobs outside of factory work...as to how to get better at it I have no idea
ἔρως
ἔρως2mo ago
yes, basic math if you see, the price goes from 1.00 to 1.01 to 1.02 ... so, the last digit is like the index in an array, and the first is just a constant but since it is 2 decimal places to the right, you divide by 100 but you have to add it all together so a loop that does total += 1 + (i / 100) but for some people, it is easier to just multiply by 0.01 - which is the same as dividing by 100, as you learned about it at school
CDL
CDLOP2mo ago
Oh ok Fair enough
ἔρως
ἔρως2mo ago
this is why we are saying it is basic math oh, and at the end you just add everything and that's the cost of sending n messages
CDL
CDLOP2mo ago
Gotcha My diet sucks and is heavily caffeinated, I blame this for the reason im a dumbass lately lol. Let’s try lower the caffeine for a bit 👀
ἔρως
ἔρως2mo ago
yeah, and sleep better
CDL
CDLOP2mo ago
I get 8 hours of sleep a night, I just rely on sugar snacks to get me through the day lately. 3-4 coffees a day, few chocolate bars/sweet bars and 1-3 Coke Zero cans It’s super bad lol
ἔρως
ἔρως2mo ago
that is not just super bad: that is horrible
Rägnar O'ock
Rägnar O'ock2mo ago
that's way too much caffeine
CDL
CDLOP2mo ago
It’s copious amounts of sugar/glucose Probably the leading cause to my coding consistency lol, chasing sugar and dopamine (chocolate and gaming)
ἔρως
ἔρως2mo ago
dude, i eat really unhealthily but i still include fruit and vegetables in my diet
CDL
CDLOP2mo ago
I have veggies for dinner but that’s about it I’ll change 😤
ἔρως
ἔρως2mo ago
you should
vince
vince2mo ago
i don't eat nearly enough fruit or veggies but going to the grocery store and trying out new stuff is always fun, maybe you'll find something you really like
CDL
CDLOP2mo ago
how in the fuck am I meant to know the solution to this was to start by assigning a variable the value of -1 HUH.
No description
ἔρως
ἔρως2mo ago
"if not found, just return -1"
CDL
CDLOP2mo ago
That confused me a lot, I initially put return “-1” 😂😂😂😂😂
ἔρως
ἔρως2mo ago
that's the error condition that's your starting state you start in the unhappy path
Rägnar O'ock
Rägnar O'ock2mo ago
Ya need to learn to break down problems into smaller parts. That's what bites you every time.
ἔρως
ἔρως2mo ago
^ exactly
CDL
CDLOP2mo ago
Yeah most likely. I breezed through up until this one and even in this one I had everything except the -1
ἔρως
ἔρως2mo ago
the -1 was very explicit there
CDL
CDLOP2mo ago
Yeah though I read that as literally return -1, so I wrote “-1” 😎
ἔρως
ἔρως2mo ago
but that is what you have to do ... and the rest of the stuff
CDL
CDLOP2mo ago
😫
ἔρως
ἔρως2mo ago
what's so confusing about the -1?
CDL
CDLOP2mo ago
My brain cannot comprehend the -1 lmao and I’m not at my pc to look at the solution anymore
CDL
CDLOP2mo ago
Trusty mobile to the rescue
No description
CDL
CDLOP2mo ago
Yeah maybe I am just being confused as the solution does I; -1, I tried doing return “-1” lol
Rägnar O'ock
Rägnar O'ock2mo ago
remember that findIndex is a thing : logs.findIndex(log => log.includes(slug));
CDL
CDLOP2mo ago
Ah… i tried indexOf lol
Rägnar O'ock
Rägnar O'ock2mo ago
I can't find a a way to golf it more... :/
function splitLogs(logs, slug) {
let i = logs.findIndex(log => log.includes(slug));
return {
before: i === -1 ? [] : logs.slice(0, i),
after: i === -1 ? [] : logs.slice(i + 1),
i,
}
}
function splitLogs(logs, slug) {
let i = logs.findIndex(log => log.includes(slug));
return {
before: i === -1 ? [] : logs.slice(0, i),
after: i === -1 ? [] : logs.slice(i + 1),
i,
}
}
CDL
CDLOP2mo ago
so there's Map, teh array method, and then there's... Map...?
Rägnar O'ock
Rägnar O'ock2mo ago
iterators
ἔρως
ἔρως2mo ago
.map <-- the array method Map <-- allows you to make a map between anything to anything
CDL
CDLOP2mo ago
yeah seems pretty cool
function addToPhonebook(phoneNumber, name, phoneBook) {
const newPhoneBook = new Map(phoneBook);
newPhoneBook.set(phoneNumber, name);
return newPhoneBook
}

// don't touch below this line

export { addToPhonebook };
function addToPhonebook(phoneNumber, name, phoneBook) {
const newPhoneBook = new Map(phoneBook);
newPhoneBook.set(phoneNumber, name);
return newPhoneBook
}

// don't touch below this line

export { addToPhonebook };
(there's code behind this code, you just don't see it)
Rägnar O'ock
Rägnar O'ock2mo ago
why do you make a new map ?
CDL
CDLOP2mo ago
part of the assignment create a copy of the input, add phoneNumber and name as key:value pairs, return the new Map so I created a copy of phoneBook, added phoneNumber => name, returned newPhonebook
Rägnar O'ock
Rägnar O'ock2mo ago
hum... ok... you can do that to make things easier to read
function addToPhonebook(phoneNumber, name, phoneBook) {
return new Map(phoneBook)
.set(phoneNumber, name);
}
function addToPhonebook(phoneNumber, name, phoneBook) {
return new Map(phoneBook)
.set(phoneNumber, name);
}
ἔρως
ἔρως2mo ago
remember that what goes in MUST BE the same type as well, or you won't get the value out
CDL
CDLOP2mo ago
good to know, looks cleaner also promises still fucking me up a year after initially learning them
CDL
CDLOP2mo ago
No description
Rägnar O'ock
Rägnar O'ock2mo ago
try to break down the assignment before going in
CDL
CDLOP2mo ago
I did, I managed it 😄
function updateMessageStatus(messageId, currentStatus, isDelivered) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (currentStatus == "Sending" && isDelivered) {
resolve(`Textio Message ${messageId} has been delivered successfully.`);
} else if (currentStatus == "Sending" && !isDelivered) {
reject(`Textio Message ${messageId} is still sending and cannot be marked as delivered.`);
} else if (currentStatus !== "Sending") {
resolve(`Textio Message ${messageId} status updated to ${currentStatus}.`)
}
}, 500);
});
}

// don't touch below this line

export { updateMessageStatus };
function updateMessageStatus(messageId, currentStatus, isDelivered) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (currentStatus == "Sending" && isDelivered) {
resolve(`Textio Message ${messageId} has been delivered successfully.`);
} else if (currentStatus == "Sending" && !isDelivered) {
reject(`Textio Message ${messageId} is still sending and cannot be marked as delivered.`);
} else if (currentStatus !== "Sending") {
resolve(`Textio Message ${messageId} status updated to ${currentStatus}.`)
}
}, 500);
});
}

// don't touch below this line

export { updateMessageStatus };
maybe long winded code wise, but it passed the test cases event loops can get in the bin
Ganesh
Ganesh2mo ago
What are you talking about
CDL
CDLOP2mo ago
oh nothing, this is jsut my thread of venting my way through bootdev apparently, lol
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Ganesh
Ganesh2mo ago
Did boot dev link that event loop video from js conf Cause that would probably help a lot if they didn't
CDL
CDLOP2mo ago
nope
Ganesh
Ganesh2mo ago
JSConf
YouTube
What the heck is the event loop anyway? | Philip Roberts | JSConf EU
JavaScript programmers like to use words like, “event-loop”, “non-blocking”, “callback”, “asynchronous”, “single-threaded” and “concurrency”. We say things like “don’t block the event loop”, “make sure your code runs at 60 frames-per-second”, “well of course, it won’t work, that function is an asynchronous ...
CDL
CDLOP2mo ago
Ohhh I remember this video!
Ganesh
Ganesh2mo ago
So they did link it?
CDL
CDLOP2mo ago
nah I saw this years ago I don't fully remember the context though so i'll watch it again
Ganesh
Ganesh2mo ago
Ah kk. It's probably the best one on YouTube, microtask queue explanation is missing but the all other concepts are similar enough
ἔρως
ἔρως2mo ago
https://www.youtube.com/watch?v=eiC58R16hb8 <-- here's another video, if you want
Lydia Hallie
YouTube
JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
Learn how the browser event loop, task queue, microtask queue, and Web APIs work together to enable non-blocking, asynchronous JavaScript. - https://www.patreon.com/LydiaHallie - https://buymeacoffee.com/lydiahallie - https://twitter.com/lydiahallie - https://www.linkedin.com/in/lydia-hallie/ - https://instagram.com/theavocoder Timestamps: 0:...
CDL
CDLOP2mo ago
I'm constantly getting messed up with setTimeout, I think it's jus the way it looks more than anything.
function startJob() {
setTimeout(() => {
console.log("Hi I'm async!");
}, 0);
console.log("Job started");
workOnJob();
}
function startJob() {
setTimeout(() => {
console.log("Hi I'm async!");
}, 0);
console.log("Job started");
workOnJob();
}
So this is runnign setTimeout on "Hi I'm async!", even though it's 0, it'll run AFTER "Job started"? and then it'll call workOnJob() oh actually, the damn course explains it lmao
ἔρως
ἔρως2mo ago
it runs after 10ms
CDL
CDLOP2mo ago
No description
ἔρως
ἔρως2mo ago
or 2ms depends on the browser
CDL
CDLOP2mo ago
yeah but with the others being immediate, it's a long enough delay (even if it shows 0)to make it go last in the queue
ἔρως
ἔρως2mo ago
setting it to 0 won't make it run any earlier i will tell you a secret this loop thingy? it doesn't matter you almost never will think about it or need to care about it
CDL
CDLOP2mo ago
fair just trying to get this bit done so I can move on lol, I'm a completionist, I can't leave the section empty but it's testing my darn patience that's for sure aight I'm through it. Runtimes and Modules left then I'm onto Typescript, finally.
ἔρως
ἔρως2mo ago
good, you're moving forward
CDL
CDLOP2mo ago
fuck sake, 'bout time
No description
CDL
CDLOP2mo ago
'ere we go
No description
CDL
CDLOP2mo ago
export function calculateTotal(
price: number,
quantity: number,
discount: number,
): number {
return price * quantity * (1 - discount);
}
export function calculateTotal(
price: number,
quantity: number,
discount: number,
): number {
return price * quantity * (1 - discount);
}
this is gonna take some getting used to!
Rägnar O'ock
Rägnar O'ock2mo ago
why is there a squiggly on a perfectly valid code ?! WHY ?!
CDL
CDLOP2mo ago
Not sure tbh, it went after i filled the empty “” with text
CDL
CDLOP2mo ago
okay someone explains this to me lol. the challenge is to create the type SupportResponse and to use it.. I managed to get it to work with an arrow function and when asking the helper (ai) to create it using a normal function, it doesn't even use SupportResponse...?
export type SupportResponse = (name: string) => string;


export const greetCustomer: SupportResponse = (name) => {
return `Hello ${name}, welcome to Support.ai! How can I assist you today?`;
};


export const farewellCustomer: SupportResponse = (name) => {
return `Goodbye ${name}, have a great day!`;
};
export type SupportResponse = (name: string) => string;


export const greetCustomer: SupportResponse = (name) => {
return `Hello ${name}, welcome to Support.ai! How can I assist you today?`;
};


export const farewellCustomer: SupportResponse = (name) => {
return `Goodbye ${name}, have a great day!`;
};
No description
CDL
CDLOP2mo ago
Is it TS being smart and matching SupportResponse to the function? "hey this is taking name as a string and returning a string, it must be SupportResponse"
Rägnar O'ock
Rägnar O'ock2mo ago
typescript matches types by their shapes, not by their names. so yes it's "hey it look the same, must be the same thing" callable types (functions) are basically only ever used for callback tho
Rägnar O'ock
Rägnar O'ock2mo ago
if your training stuff didn't go over how typescript works you might want to look at the typescript into on their doc : https://www.typescriptlang.org/docs/handbook/intro.html
Handbook - The TypeScript Handbook
Your first step to learn TypeScript

Did you find this page helpful?