The Student Room Group

Computer science undergrads help! (javascript)

Write a function to return a string that contains random lowercase alphabetic
characters where the length must be equal to the value of the parameter. The
second parameter contains an array of letters to omit.

Can you tell me how to approach this problem?
Original post by nanachan123
Write a function to return a string that contains random lowercase alphabetic
characters where the length must be equal to the value of the parameter. The
second parameter contains an array of letters to omit.

Can you tell me how to approach this problem?


So I don’t know JavaScript I’m afraid, but I could give you some pointers of what you may need and the flow of it.

I’d say make a function with the parameters ‘length’ and ‘letters’ where length is an Int and letters is an Array of Char.

Inside the function you want a variable called OutputStr or similar, and a variable called Count (which will be the current length of the string)

Then...

While Count <= length :
Count 1
// select a random index of the array
index = Int.random(max: length)
outputStr = letters(index).lowercased
End While
return outputStr

does this make sense? I know it’s not JavaScript but it’s the general workflow of this function I believe? Assuming I’m understanding the question correctly?
(edited 4 years ago)
Original post by nanachan123
Write a function to return a string that contains random lowercase alphabetic
characters where the length must be equal to the value of the parameter. The
second parameter contains an array of letters to omit.

Can you tell me how to approach this problem?


The way to approach any large, difficult or complex programming problem is using a 'divide-and-conquer' approach, which means breaking it down into pieces until you have small, simple easy pieces which you can solve.

Start with the information you've been given:

You have been asked to write a function which accepts two parameters and returns a string. Do you know how to write (and use) a function in JavaScript? If not you need to spend time on Google looking for some basic information on functions, including their syntax and usage

You have been told that the function generate a string of random alphanumeric characters, so to start with, write the code which is able to generate just ONE single alphanumeric character by itself.

- Then, try writing some code which can generate TWO alphanumeric characters, and joining them together as a string
- Then, try writing some code which can generate THREE alphanumeric characters, and joining them together as a string
- Then, try writing some code which can generate FOUR alphanumeric characters, and joining them together as a string

When you're writing that code, look carefully at the similarities (pattern) between each of the solutions for one, two, three and four characters. Ask yourself how you might be able to generalise that pattern so that it can work for any variable number of alphanumeric characters; consider the programming concept of repetition for this; again do some Google'ing to find out how to get JavaScript to perform repetition if you're not sure on the syntax/structure. (You might consider the "for" keyword, because that would be a good fit for a problem like this).

Secondly, once you've gotten the basics working for your code to generate this random string, start looking at how to check whether one of your randomly generated characters is in that the list.
You might consider google'ing for questions such as
- How do I compare character values in JavaScript?
- How do I check whether an array contains a particular value in JavaScript?


Remember that the best way to solve problems is incrementally, and doing small bits at a time. Make use of console.log() so that you can see the output of your program at each step of the way and what it's doing.
omg you're awesome, thank you!
Original post by winterscoming
The way to approach any large, difficult or complex programming problem is using a 'divide-and-conquer' approach, which means breaking it down into pieces until you have small, simple easy pieces which you can solve.

Start with the information you've been given:

You have been asked to write a function which accepts two parameters and returns a string. Do you know how to write (and use) a function in JavaScript? If not you need to spend time on Google looking for some basic information on functions, including their syntax and usage

You have been told that the function generate a string of random alphanumeric characters, so to start with, write the code which is able to generate just ONE single alphanumeric character by itself.

- Then, try writing some code which can generate TWO alphanumeric characters, and joining them together as a string
- Then, try writing some code which can generate THREE alphanumeric characters, and joining them together as a string
- Then, try writing some code which can generate FOUR alphanumeric characters, and joining them together as a string

When you're writing that code, look carefully at the similarities (pattern) between each of the solutions for one, two, three and four characters. Ask yourself how you might be able to generalise that pattern so that it can work for any variable number of alphanumeric characters; consider the programming concept of repetition for this; again do some Google'ing to find out how to get JavaScript to perform repetition if you're not sure on the syntax/structure. (You might consider the "for" keyword, because that would be a good fit for a problem like this).

Secondly, once you've gotten the basics working for your code to generate this random string, start looking at how to check whether one of your randomly generated characters is in that the list.
You might consider google'ing for questions such as
- How do I compare character values in JavaScript?
- How do I check whether an array contains a particular value in JavaScript?


Remember that the best way to solve problems is incrementally, and doing small bits at a time. Make use of console.log() so that you can see the output of your program at each step of the way and what it's doing.

Quick Reply

Latest

Trending

Trending