For excercises

TIK TOK CHINA exercise.

Create a JS program that gets a number from the user and then print the numbers from 0 to the number provided by the user and do the following calcuations.

1- if the number is divisible by 3 it should print TIK instead of the number.

2- If the number is divisible by 5 it should print TOK instead of the number.

3- If the number is divisible by 3 and 5 it should print CHINA instead of the number.

4- Use prompt to get the value from the user.

5- Use console.log to print the output to the console.

Examples of when user inputs 20

1
2
TIK
4
TOK
TIK
7
8
TIK
TOK
11
TIK
13
14
CHINA
16
17
TIK
19
TOK

2 cristmas tree

Write a program that receives a value from a user using prompt and draw a christmas tree using * and spaces with the same level of lines as the number inserted by the user.

Example when user inputs 5

    *
   ***
  *****
 *******
*********

3 Enigma machine

Create a program that can encrypt messages using the following rules

1-The program should ask if it needs to encrypt or decrypt

2- the program will receive a phrase in english in lower case

3. the program should replace the letters as follows table, (depending if is encrypting or decrypting.

4 any other character should remain unchanged.

q — aw — be — cr — dt — ey — f
u — g i — ho — ip — ja — ks — l
d — mf — ng — oh — pj — qk — r
l — sz — tx — uc — vv — wb — x
n — ym — z

example, if the program receives the string “cat” it should return “vke“. when encrypting

In the same way, if the program gets the string ‘vke’, it should return ‘cat’ if decrypting

NOTES:

if you have a variable string

var str = “hello”

you can get characters at specific part of the string with

str.charAt(2)

you can get the length of the string with

str.length

4 Roman Nightmare

Make a program that translates a number provided by the user between 1 and 399 and translates it to roman numbers.

Example when inserting 7 output should be VII

Example when inserting 399 output should be CCCXCIX

Example when inserting 159 output should be CLIX

Notes:, in roman numbers the following equivalences hold true

I = 1

V = 5

X = 10

L = 50

C = 100

III = 3

XXX = 30

III = 300

for loop basics

A for loop allows to execute a piece of code several times.

Example 1.0

for(var i =0; i<10; i++)
{
    console.log("I am printing this message 10 times");
}

If the previous code where to run in chrome we could see the following output in the console.

Basic structure of for loop

for(    INITIALIZATION     ;     CONDITION     ;    INCREMENT    )
{
           CODE
}

We have 4 main parts that build the for

  • INITIALIZATION: Executed only one time at all.
  • CONDITION: Executed before the code is executed. (executed several times)
  • CODE: Executed only if CONDITION is meet (executed several times)
  • INCREMENT: Executed after the code is executed. (executed several times)

The following diagram shows how the flow of a for is performed.

so in our original example 1.0 we could spread the parts of the for as follows.

for(var i =0; i<10; i++)
{
    console.log("I am printing this message 10 times");
}

NOTE THAT

  • INITIALIZATION executes only once.
  • the CONDITION will be executed as long as it is meet.
  • Every time the CONDITION is meet, the CODE and the INCREMENT will execute once.