The Student Room Group

JavaScript help please?

How would you code the following:
"Flip" returns for every number where a 3 appears (ie 3,13,23 etc) would return Flip
"Blop" returns for every number where a 5 appears ie (5,15,25,35..)

the rest return as their ordinary self.
?
ie)
1,2,flip,4,blop,6,7,8,9,10,11,12,flip etc
Original post by Dancatpro
I'll get you started with some pseudocode.

If x/3 is an integer and x/5 is an integer then return ???? (I know there is another word you have missed for this game)

ElseIf X/3 is an integer then return flip

ElseIf X/5 is an integer then return blop

Else Return X

End If

________________

I'm not on TSR much so you will have to get someone else to answer questions


You have misunderstood the question.
Original post by bobbyideye
How would you code the following:
"Flip" returns for every number where a 3 appears (ie 3,13,23 etc) would return Flip
"Blop" returns for every number where a 5 appears ie (5,15,25,35..)

the rest return as their ordinary self.
?
ie)
1,2,flip,4,blop,6,7,8,9,10,11,12,flip etc


What happens when a number contains both a 3 and a 5?

Cycle through the digits using the remainder operator (%10), checking if they are 3 or 5.
Reply 3
Put the lot in as strings in an array; check each element using an if block which uses the charAt() method to check for '3' and '5' and replaces the string appropriately, with this block nested in a for loop which iterates over each character in the string, which itself is nested in a for loop which iterates over each string in the array.

Done. Quite a simple algorithm.

P.S using numerical methods is an utter waste of time. You have already said that you are working with strings, and so you should look at string-based solutions to your problem.
(edited 8 years ago)
Reply 4
You could try using x%10==3 and x%10==5 to take care of the units column. To check the 10s column, you could try if (math.floor(x/10))%10==3 and similarly for 5. (I think this works. Haven't done any JavaScript in a while). The same method should work for 100s columns and above.


Posted from TSR Mobile
Reply 5
Original post by Joel_tsr
You could try using 1)x%10==3 and 2)x%10==5 to take care of the units column. To check the 10s column, you could try if (math.floor(x/10))%10==3 and similarly for 5. (I think this works. Haven't done any JavaScript in a while). The same method should work for 100s columns and above.


Posted from TSR Mobile


1) Solutions are x = 13 + 10n, where n is a positive integer.
2) Solutions are x = 15 + 10n, where n is a positive integer.

The meaning of this is that there is no single-digit solution to either of these equations - they cannot possibly provide the correct solutions of 3 and 5 respectively.
Reply 6
For anyone thinking this is a FizzBuzz problem it isn't.

Just convert the int to a string and check if the string contains a 3 or a 5 and then change the string to something else, then you loop through all your strings and print them out.
Original post by VannR
1) Solutions are x = 13 + 10n, where n is a positive integer.
2) Solutions are x = 15 + 10n, where n is a positive integer.

The meaning of this is that there is no single-digit solution to either of these equations - they cannot possibly provide the correct solutions of 3 and 5 respectively.


You're wrong.
Reply 8
Original post by Jooooshy
You're wrong.


Nah.
Original post by VannR
Nah.


3%10 = 3
Reply 10
Original post by Jooooshy
3%10 = 3


The % (modulus) operator returns the integer remainder produced in the calculation LHS / RHS. Therefore, 3%10 = 0 since 3/10 is less than 1 and the integer value would probably be truncated to 0 in a program. Even if you were to write this as 10%3, you would receive a value of 1 since 3 goes into 10 3 times, leaving an integer remainder of 1.
Original post by VannR
The % (modulus) operator returns the integer remainder produced in the calculation LHS / RHS. Therefore, 3%10 = 0 since 3/10 is less than 1 and the integer value would probably be truncated to 0 in a program. Even if you were to write this as 10%3, you would receive a value of 1 since 3 goes into 10 3 times, leaving an integer remainder of 1.


You're right, it's the integer remainder. What's the remainder of 3/10? It's 3. Put it into a REPL if you really don't believe me..
Reply 12
Original post by Jooooshy
You're right, it's the integer remainder. What's the remainder of 3/10? It's 3. Put it into a REPL if you really don't believe me..


Fuuuu*k.

It goes in 0 times remainder 3. I get what you mean now :biggrin:.

Having said that, I still hold to the view that string methods are the best way to solve OP's problem, but thank you for pointing this out to me.

Quick Reply

Latest

Trending

Trending