Online worksheet ยท 3 of 4

Separate the algorithm from its syntax - Online practice 3

Read the information, then answer the questions. An adult may read the instructions aloud. Show your working where asked.

Read before answering

Compare numeric algorithms. Python: def f(n): return n*4+3. JavaScript: function f(n) { return n*4+3; }. Test numeric input 2, then text input '2'. Python text multiplied by an integer repeats the text, but adding an integer to that text fails. JavaScript's multiplication converts this numeric text before adding. Use an approved local sandbox, never personal inputs.

Worksheet preview

  1. For numeric input 2, what should both functions with n*4+3 return?
  2. What happens for text '2' in each language under these exact snippets?
  3. How should the contract avoid depending accidentally on those conversion differences?

This exercise is included in the free preview. Register free to complete it and download the preview PDFs. No payment is required.

Register free

Already registered? Sign in