Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,350 members, 7,957,931 topics. Date: Wednesday, 25 September 2024 at 03:28 AM |
Nairaland Forum / Science/Technology / Programming / Can Someone Help Me With With This Javascript Exercise Please (856 Views)
HELP With This Javascript Problem / Please Help With This Javascript Code / Help Me With This Javascript Problem (2) (3) (4)
Can Someone Help Me With With This Javascript Exercise Please by Winningbot: 1:10pm On Jun 13, 2022 |
Pls I just need explanation on number 2 only. I can only get the length of the first sentence but I can't get the length of the first
|
Re: Can Someone Help Me With With This Javascript Exercise Please by Extratyre01(m): 1:21pm On Jun 13, 2022 |
Winningbot: Message me in the number on my signature |
Re: Can Someone Help Me With With This Javascript Exercise Please by Xedmark: 1:24pm On Jun 13, 2022 |
Use this solve number one question function WordCounter (str) { var words = str.split(" ".length; return words; } However use the name of the string class in the bracket after the wordcounter. You can chat me up for the rest. |
Re: Can Someone Help Me With With This Javascript Exercise Please by Winningbot: 1:25pm On Jun 13, 2022 |
Extratyre01:okay boss |
Re: Can Someone Help Me With With This Javascript Exercise Please by Belkid01(m): 2:20pm On Jun 13, 2022 |
Extratyre01: Function wordCounter (str) { let eachWord = str.split( "" ) ; return eachWord [1]. length; } Explanation: The String split method splits a strings with respect to the argument passed in it. In our case ( "" ) : i.e. anywhere it finds a space( "" ) it will split/cut and returns the splited items in an array.... For example str = "I am a programmer" str.split( "" ) returns = ["I", "am", "a", "programmer"] //Splitting the sentence everywhere it finds an empty space... we save the returned array in a variable = eachWord Then we simply target the second index/second word using square bracket methods.... eachWord [1]. //index in JS starts from 0 Then we get the length.... With the "length" property = eachWord [1]. length |
Re: Can Someone Help Me With With This Javascript Exercise Please by Belkid01(m): 2:27pm On Jun 13, 2022 |
Xedmark: This will return the length of items in the array... In this case, the total number of "words" in the sentence... |
Re: Can Someone Help Me With With This Javascript Exercise Please by gistray: 3:01pm On Jun 13, 2022 |
Belkid01: Your Solution no work |
Re: Can Someone Help Me With With This Javascript Exercise Please by Nobody: 3:11pm On Jun 13, 2022 |
gistray: Ok you didnt actually use " . " because nairaland changes it to emoji |
Re: Can Someone Help Me With With This Javascript Exercise Please by Nobody: 3:21pm On Jun 13, 2022 |
Solution 1: //The .split method splits your string when ever it identifies what you passed into it and then forms an array Now assuming the sentence here ends when ever we get a ' . ' function NoStringsAttached(){ const myStr = 'This is my test string to practice the javascript string function concepts. This is gonna be fun' return myStr.split("." )[0].length } Now Run: NoStringsAttached() Solution 2: //Same thing for No2. Its .split method splits your string when ever it identifies what you passed into it and then forms an array function NoStringsAttached(){ const myStr = 'This is my test string to practice the javascript string function concepts. This is gonna be fun' return myStr.split("." )[1].length } Now Run: NoStringsAttached() |
Re: Can Someone Help Me With With This Javascript Exercise Please by Nobody: 3:24pm On Jun 13, 2022 |
Can also be done with for loops. just build a string with each letter in the strings and when you get to " . " break the string then start building a new letter, then push them to and array then return length of first or second of your array element |
Re: Can Someone Help Me With With This Javascript Exercise Please by Nobody: 3:26pm On Jun 13, 2022 |
Winningbot:Look up string methods on w3schools its impossible to remember all the methods For the search you will use JavaScript regEx find it on w3schools, every JS method is explained there I have the site locally on my laptop and I'm using it rn for a little Js game I'm attempting |
Re: Can Someone Help Me With With This Javascript Exercise Please by thanksjosh005: 5:11pm On Jun 13, 2022 |
The author of this thread might need to clarify his post to get a suitable answer. We understand when you say: "Pls I just need explanation on number 2 only" but you also went on to say: "I can only get the length of the first sentence but I can't get the length of the first" What you're are asking for is not clear enough or juxtaposed but I just want to assume it's a typo and you mean second instead of first @ the bolded. However, you can use the .splice () method and collect the length of the entire character when you output your code. The .splice() is another kind of procedure to achieve your result. I know different programmers have different procedures. So there are various ways you can achieve what you're asking for. |
Re: Can Someone Help Me With With This Javascript Exercise Please by Nobody: 5:33pm On Jun 13, 2022 |
thanksjosh005: .splice() is an array method in JavaScript and not for strings. Unless ur talking about another language 1 Like |
Re: Can Someone Help Me With With This Javascript Exercise Please by daHcKR: 6:16pm On Jun 13, 2022 |
mstr.split("."[1].length; Should do the trick. Here you are splitting the string using the full stop as the separator, which returns an array of the split. Index number 1 represents the second sentence. |
Re: Can Someone Help Me With With This Javascript Exercise Please by daHcKR: 6:17pm On Jun 13, 2022 |
thanksjosh005:The splice method is for arrays |
Re: Can Someone Help Me With With This Javascript Exercise Please by thanksjosh005: 6:33pm On Jun 13, 2022 |
My oh my.... I guess I juxtaposed, too. I was referring to .slice() not .splice() then using the .length to collect the amount of string character. |
Re: Can Someone Help Me With With This Javascript Exercise Please by divine404: 7:06pm On Jun 13, 2022 |
solution 2: // Get the index of the first letter of the 2nd sentence. const index = myStr.lastIndexOf("T" // Slice it and get the length. const result = myStr.slice(index).length; console.log(result); |
Re: Can Someone Help Me With With This Javascript Exercise Please by Deicide: 12:54pm On Jun 14, 2022 |
Just split by full stop. Though I would like to see how you got the length of the first Sentence, cause if you knew that, why would getting the second sentence be a problem?
|
(1) (Reply)
Please Let Someone Help Me Out With My Outlook Programming / Sap/oracle User Companies / A Computer Programmer To Be Need Help Seriously! Please Anybody
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30 |