Ten_ IPO_Exercises (2)

advertisement
Ten Simple Javascript Exercises
1. Define a function max() that takes two numbers as arguments and returns the largest
of them. Use the if-then-else construct available in Javascript.
Input
Read x, y
Process
If x=>y
max=0
Then max=x
Output
Print (or Display) Max
Else max=y
End
2. Define a function maxOfThree() that takes three numbers as arguments and returns
the largest of them.
Input
Read x, y
Process
If x>=y
max=0
(If x>=z then max =x else
max =z)
Output
Print (or Display) Max
Else (If y>=z then max=y
else max=z)
End
3. Write a function that takes a character (i.e. a string of length 1) and returns true if it
is a vowel, false otherwise.
Input
Read string
Process
Taka a random letter of the
string
If letter= vowel, return =true
Else, return = False
End
Output
Print (or Display) Return
4. Write a function translate() that will translate a text into "rövarspråket". That is,
double every consonant and place an occurrence of "o" in between. For example,
translate("this is fun") should return the string "tothohisos isos fofunon".
Input
Read string
Process
For i=0 to length of string
Output
Print (or Display) return
If letter (i) of string = vowel
Return = return + letter (i)
Else
Return = return + letter
(i)+o+letter(i)
End if
5. Define a function sum() and a function multiply() that sums and multiplies
(respectively) all the numbers in an array of numbers. For example, sum([1,2,3,4])
should return 10, and multiply([1,2,3,4]) should return 24.
Input
Read array of number
Process
If function=sum
Return =
num1+num2+num3+…+numn
If function=multiply
Return =
num1*num2*…*numn
End
Output
Print (or Display) Sum or
Multiply
6. Define a function reverse() that computes the reversal of a string. For example,
reverse("jag testar") should return the string "ratset gaj".
Input
Read string
Process
Count = length of string
Output
Print (or Display) Return
For i=1 to length of string
Return = return + letter
(count ) of string
Count = Count -1
End
7. Represent a small bilingual lexicon as a Javascript object in the following fashion
{"merry":"god", "christmas":"jul", "and":"och", "happy":gott",
"new":"nytt", "year":"år"} and use it to translate your Christmas cards from
English into Swedish.
Input
Read string
Process
Replace merry by god
Output
Print (or Display) string
Replace Christmas by jul
……
Replace year by ar
End
8. Write a function findLongestWord() that takes an array of words and returns the
length of the longest one.
Input
Read array of string
Process
Find length of each word
Compare the Length
Return = word with largest
length
End
Output
Print (or Display) Return
9. Write a function filterLongWords() that takes an array of words and an integer i
and returns the array of words that are longer than i.
Input
Read Array of string
Process
Find length of each string
Read Filter value
If length > filter value
Output
Print (or Display) display
Display =yes
End
10. Write a function charFreq() that takes a string and builds a frequency listing of the
characters contained in it. Represent the frequency listing as a Javascript object. Try it
with something like charFreq("abbabcbdbabdbdbabababcbcbab").
Input
Read string
Process
Find length of string
Output
Print (or Display)
Freqofa =0
For i=1 to length of string
Freqofa
(If letter(i)of string = a
Freqofb
Freqofa = Freqofa+1)
Freqofb = 0
For i=1 to length of string
(If letter(i)of string = b
Freqofb = Freqofb +1)
End
Download