My solution: const sample = "He is a very very good boy, ins't he?"; const tokenise = (str) => { let output = str.split(/[\s!,?._'@]/).filter((w) => w.length > 0); output.unshift(output.length); return output.join(" "); }; console.log(tokenise(sample));
@surayamelina Жыл бұрын
So glad I found your channel! This is exactly the kind of video I've been looking for. Great to see an example of what you'd do in a real interview setting and how you talk through all your questions/uncertainties. In my interview prep that's what I've been really curious about, what does it actually look like to solve a problem in real time - glad I'm not the only one asking myself a lot of questions and changing my mind as I go!
@JamesQQuick Жыл бұрын
So glad you enjoyed it!
@RetroMan432111 ай бұрын
Extremely helpful to have a human explain through the often confusingHackerRank interface.
@komald70482 жыл бұрын
This is a great video, breaking down the thought process on solving these problems. I like the way you went from for loops to more modern using .map() I am looking forward to more.
I think what is so valuable about this video is that you still expressed confidence in figuring out the problem even when you felt unsure about how something work (e.g. regex). I think how I would approach is talking through the entire problem before coding while I am walking through the problem. Regardless, this video demonstrated break downs of thought process which I found valuable. Kudos!
@JamesQQuick Жыл бұрын
Yeah agreed! Thanks so much for watching :)
@RetroMan432111 ай бұрын
Yes it's modeling confidence despite initial uncertainty. This is the goal.
@JohnDoeX19662 жыл бұрын
This is absolutely great. Could you do more JavaScript interview prep videos?
@JamesQQuick2 жыл бұрын
That's the plan :)
@adelaykay2 жыл бұрын
I've turned on notifications for all videos
@JamesQQuick2 жыл бұрын
@@adelaykay Yay!
@wadecodez2 жыл бұрын
Personally I would have used a single loop and pushed items to an array of arrays. Each time a special character occurs I would start a new array. It would be slightly more efficient because split, join, and filter all imply another loop. Good video though.
@JamesQQuick2 жыл бұрын
Nice! Thanks for sharing your approach. The good thing is that a finite number of loops (const value) is relatively negligible in terms of Big O, but keeping everything in one loop is a nice tough!
@shakeebarsalan70302 жыл бұрын
@@JamesQQuick Theme name please.
@xynyde02 жыл бұрын
@@shakeebarsalan7030 purple
@avertry95292 жыл бұрын
I'll post my answer before watching. Harder than it looks. function res(string) { const split = string.split(/[\W\d]/g) console.log(split.length.toString()) split.map((r) => r != '' && console.log(r)) } res("How are you today, isn't it a lovely day, better than the last 3 days?")
Two things I personally would’ve done differently. One is that I tend to use backticks to encompass strings when it’s convenient to avoid issues with literal strings containing single or double quotes; circumvents annoying, cumbersome-looking escapes. The second is that I probably would’ve tried replacing the special characters by doing a forEach loop on the special characters array and running a replaceAll on the source string for each. Great vid! These sorts of tests put developers on the spot, which can be uncomfortable, for sure. But I think it’s great to show the process and how you personally work through it.
@JamesQQuick2 жыл бұрын
Love it. Thanks for sharing :)
@jakubwikacz45582 жыл бұрын
Hi James, I have skipped the video this time after few minutes. Consider learning regexp as that is a good tool which you can use everywhere (frontend, backend, sql, searching in text files, converting texts). When you master regexps then you will have another tool in your hands, which will save time :) I do press a like button for a raw approach and a courage to share it as a video :)
Thank you, I have an interview in 30 minutes. wish me luck!
@JamesQQuick Жыл бұрын
Good luck! Let me know how it goes!!
@paraschauhan99782 жыл бұрын
What is the difference between instance and object??🤔
@JamesQQuick2 жыл бұрын
Well, an instance is typically associated with Object Oriented Programming, where you have an instance of a class. The class is the definition for what an object looks like and the instance is the actual object itself. An object in JavaScript is basically anything that is not a primitive value.
@christopheanfry24252 жыл бұрын
Very instructive video. I think you did it well on your first try but you just forgot to add blank space in your special character variable. Maybe I’m wrong i don’t have the experience you have. 😉
@JamesQQuick2 жыл бұрын
Well, I don't want to consider a blank space as a special character since it becomes the delimeter when I call .split() at the end.
@christopheanfry24252 жыл бұрын
@@JamesQQuick got it thanks!!
@tamantaman2 жыл бұрын
Thanks
@suelingsusu13392 жыл бұрын
Convert string to array use the destructuring operator..... const stringArray = [...stringVariable]
My only comment is you didn’t verify that the solution was still correct after using the Set approach. I was waiting for you to show the full output at the end because “very” appears twice in the input so did that still come out correctly?
@kyleskincoug2 жыл бұрын
The set was only to contain the special characters for speeding up comparing the token characters to those. The output was still just array so duplicates should still be present.
@JamesQQuick2 жыл бұрын
+1 to this. Great question though Jess Daniel. I'm glad you called out that I should have re-tested!
@ming39572 жыл бұрын
Hackerman James
@stevenvallarsa17652 жыл бұрын
Did I miss an instruction not to use RegEx to split the string? const tokens = s.split(/[ !.,_'@?]+/) in JavaScript.
@MC---2 жыл бұрын
This is the optimal way of doing it. When watching the video I was not 100% sure split could take a RegEx but thought it was likely. I think the global flag is required for cases where there are multiple instances of the special characters.
@Tomapon2 жыл бұрын
It's fine to use regex but many people don't know it perfectly without looking it up (like in an interview situation as he mentioned). I think something like .filter(n => n) needs to be added to your solution in order to remove the last empty item in the array? If I only do s.split(/[ !.,_'@?]+/) the array has an empty item in the end, but if I do const tokens = s.split(/[ !.,_'@?]+/).filter(n => n) it seems fine.
@JamesQQuick2 жыл бұрын
Yeah my take is that many people can't write regex from scratch/memory which is true for me. I thought it would be valuable to show people how I would handle the situation knowing that I couldn't come up with regex on the spot. Hopefully the thought process and being able to adapt is beneficial. I agree though, regex would be a great fit!
@gaborkovacs1892 жыл бұрын
@@Tomapon I think your regex is ok, but filter the whole array is unnecessary, because empty element can occur only at the beginning and at the end.
@DavidBcc Жыл бұрын
Seriously? No string.match()?
@braveitor2 жыл бұрын
Another approach: const specialChars = new Set(['!', ' ',',', '?', '.', '_', '\'', '@']) const printTokens = (str, specialChars) => { let tempArray = []; let tempString = ''; for (let i = 0; i < str.length; i++) { const letter = str[i]; if (!specialChars.has(letter)) { tempString += letter } else if (letter!='') { tempArray.push(tempString) tempString = ''; } } return(tempArray.filter(t=>t)); } let filtered = printTokens("He is a very good boy, isn't he?", specialChars) console.log(filtered)
@omerg0101 Жыл бұрын
you can use the ASCII values when checking the charecters like this: function printTokens(s) { const tokens = []; let token = ''; s.split('').forEach(char => { if ((char >= 'a' && char = 'A' && char console.log(token)); } printTokens("He is a very very good boy, isn't he?")
@peteriannuzzo15257 ай бұрын
"He is a very very good boy, isn't he?".replace(/^[^A-Za-z]+/, '').replace(/[^A-Za-z]+$/, '').split(/[^A-Za-z]+/g) .replace(s) will remove all unwanted characters at the beginning and end of string prior to splitting string.
@jasonrm9992 жыл бұрын
const solution = str => { const strArr = str .replace(/[!,?._'@]/g, ' ') .replace(' ', ' ') .trim() .split(' '); console.log(strArr.length); strArr.forEach(str => console.log(`${str} `)); }; solution(`He is a very very good boy, isn't he?`); Probably missing something (newbie), but it worked with examples I tested with.
@lionelt872 жыл бұрын
the .replace() call only replaces the first occurence of a string, so only the first double space gets replaced to a single one. only workaround is to use regex with 'g' (global) flag : 'aa aa aa'.replace(/aa/g, 'bb')
@jasonrm9992 жыл бұрын
@@lionelt87 Yeah I goofed, should have been .replaceAll() (I think that would work) or a regex.
@lionelt872 жыл бұрын
@@jasonrm999 thanks for the replaceAll mention, i nearly forgot it exists!
const inputString = "He is a very very good boy, isn't he?"; function splitTokens(inputString) { let tokens = inputString.split(/[!,.@'?\s]+/); return tokens.filter(token => token !== ""); } function printTokens(tokenArr) { console.log(tokenArr.length); for (let token of tokenArr) { console.log(token); } } printTokens(splitTokens(inputString));
@JamesQQuick2 жыл бұрын
Yeah it would help if I could write Regex from scratch which I can't, and I don't think many people can honestly. I think its valuable to show people how you can still solve a problem intelligently in this type of scenario if the regex isn't accessible. That's a great solution though!
@aham_sammich2 жыл бұрын
@@JamesQQuick Of course! Thank you for the video! Your channel has taught me a lot and helped me through several projects recently.
const str = "He is a very very good boy, isn't he?" const tokens = str.split(/[^A-Za-z]/).filter(x => x != "") console.log(tokens.length) tokens.forEach(x => console.log(x))