×

Challenges

  1. Not started

  2. Not started

  3. Not started

  4. Not started

  5. Not started

  6. Not started

  7. Not started

  8. Not started

  9. Not started

  10. Not started

  11. Not started

  12. Not started

  13. Not started

Challenge 4.5

Count dots on any number of black and white cards as one input (using a loop)

Challenge Level: Ready to expand

Requirement:

Write a program that asks the user to enter any number of black and white cards representing bits, all as one input ('B' for black and 'W' for white), and displays the total number of dots as the output.

Hints
  • Make variables called:

    • “total number of dots” and set its value to 0.
    • “number of dots” and set its value to 1.
    • “cards” and set its value to the string entered as the input. Check each letter of the input string “cards” (use a loop to iterate the number of letters in the input) starting from the last letter. If it’s ‘W’ add the corresponding number of dots (1 for the last letter, 2 for the second to last letter and so on) to the “total number of dots”. Display the “total number of dots” as the output.
    • “index” and set its value to the number of letters in the input. Use this variable to access a letter at the “index” position in the string.
  • You can access a letter at the specified position in a string by using the 'Letter of' block under “Operators”.

  • In this challenge you need to access all the letters in user’s input and check to see if each of them is equal to B (black) or W (white).

  • You can find how many letters a string has by using the 'Length of' block under “Operators”.

  • The 'If' block checks if the condition is true and then runs the blocks inside of the IF block. In this challenge you need to check if each letter of the input is equal to ‘W’ or “B’.

Programming Reminders

Output statements

Output a string directly
Blocks showing how to output a string
Output a variable
Blocks showing how to output a variable

Variables

Set a variable as a string
Blocks showing how to set a variable as a string
Set a variable as an integer
Blocks showing how to set a variable as an integer
Set a variable from a calculation
Blocks showing how to set a variable from a calculation
Set a variable from a user input (text)
Blocks showing how to set a variable from a user input (text)
Set a variable from a user input (number)
Blocks showing how to set a variable from a user input (number)
Add one to a value
Blocks showing how to add one to a value

Conditionals

Find out the discount on fruit
Blocks showing how to use conditional blocks

Loops

Output string 10 times
Blocks showing how to output a string
Output number until number reaches 10
Blocks showing how to output a string
Recommended Blocks
Number block
String block
Ask and wait for text block
Say block
Multiply block
Add block
Subtract block
Equality block
Length of string block
Letter of string block
Set variable to block
Custom variable block
Repeat block
If block

Create your program in the editor below

Your submission results will be displayed here

Input Expected output Received output Status
WBB
4

                
Not yet run ?
W
1

                
Not yet run ?
B
0

                
Not yet run ?
WWWWWWWW
255

                
Not yet run ?