Practice For Javascript
Practice For Javascript
Sample Solution : -
<body onload="reverse()">
<script>
function reverse()
{
var r=prompt("ente the number");
var t=r.split('').reverse().join('');
document.write(t);
}</script>
</body>
</html>
<body onload="alphabet_order()">
<script>
function alphabet_order(str)
{
return str.split('').sort().join('');
}
document.write(alphabet_order("webmaster"));</script>
</body>
</html>
www.mrsaem.com 1|Page
www.mrsaem.com 1|Page
A-LEVEL IT
Practice More JavaScript (9626)
Note : As the letter 'y' can be regarded as both a vowel and a consonant, we do
not count 'y' as vowel here.
Sample Data and output:
Example string : 'The quick brown fox'
Expected Output : 5
1. function vowel_count(str1)
2. {
3. var vowel_list = 'aeiouAEIOU';
4. var vcount = 0;
5.
6. for(var x = 0; x < str1.length ; x++)
7. {
8. if (vowel_list.indexOf(str1[x]) !== -1)
9. {
10. vcount += 1;
11. }
12.
13. }
14. return vcount;
15. }
16. Document.write(vowel_count("The quick brown fox"));
JavaScript Code :
view plaincopy to clipboardprint?
www.mrsaem.com 2|Page
www.mrsaem.com 2|Page
A-LEVEL IT
Practice More JavaScript (9626)
3. function uppercase(str)
4. {
5. var array1 = str.split(' ');
6. var newarray1 = [];
7.
8. for(var x = 0; x < array1.length; x++){
9. newarray1.push(array1[x].charAt(0).toUpperCase()+array1[x].slice(1))
;
10. }
11. return newarray1.join(' ');
12. }
13. document.write(uppercase("the quick brown fox"));
JavaScript Code :
view plaincopy to clipboardprint?
www.mrsaem.com 3|Page
www.mrsaem.com 3|Page
A-LEVEL IT
Practice More JavaScript (9626)
22. document.write(today);
1. today=new Date();
2. var cmas=new Date(today.getFullYear(), 11, 25);
3. if (today.getMonth()==11 && today.getDate()>25)
4. {
5. cmas.setFullYear(cmas.getFullYear()+1);
6. }
7. var one_day=1000*60*60*24;
8. document.write(Math.ceil((cmas.getTime()-today.getTime())/(one_day))+
9. " days left until Christmas!");
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time
value that is the number of milliseconds since 1 January, 1970 UTC. You can
declare a date in the following ways :
new Date();
new Date(value);
new Date(dateString);
The getFullYear() method is used to get the year of the specified date according
to local time. The value returned by the method is an absolute number. For dates
between the years 1000 and 9999, getFullYear() returns a four-digit number, for
example, 1985.
The getMonth() method is used to get the month in the specified date according
to local time, as a zero-based value. The value returned by getMonth() is an
integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.
The getDate() method is used to get the day of the month for the specified date
according to local time. The value returned by getDate() is an integer between 1
and 31.
www.mrsaem.com 4|Page
www.mrsaem.com 4|Page
A-LEVEL IT
Practice More JavaScript (9626)
The getTime() method is used to get the numeric value corresponding to the time
for the specified date according to universal time.
The Math.ceil() function is used to get the smallest integer greater than or equal
to a given number.
Flowchart :
www.mrsaem.com 5|Page
www.mrsaem.com 5|Page
A-LEVEL IT
Practice More JavaScript (9626)
Sample Solution : -
HTML Code :
view plaincopy to clipboardprint?
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset=utf-8 />
5. <title>JavaScript program to calculate multiplication and division of two
numbers </title>
6. <style type="text/css">
7. body {margin: 30px;}
8. </style>
9. </head>
10. <body>
11. <form>
12. 1st Number : <input type="text" id="firstNumber" /><br>
13. 2nd Number: <input type="text" id="secondNumber" /><br>
14. <input type="button" onClick="multiplyBy()" Value="Multiply" />
15. <input type="button" onClick="divideBy()" Value="Divide" />
16. </form>
17. <p>The Result is : <br>
18. <span id = "result"></span>
19. </p>
20. </body>
21. </html>
JavaScript Code :
view plaincopy to clipboardprint?
1. function multiplyBy()
2. {
3. num1 = document.getElementById("firstNumber").value;
4. num2 = document.getElementById("secondNumber").value;
5. document.getElementById("result").innerHTML = num1 * num2;
6. }
7.
8. function divideBy()
9. {
www.mrsaem.com 6|Page
www.mrsaem.com 6|Page
A-LEVEL IT
Practice More JavaScript (9626)
Explanation :
document.getElementById(id).value : The value property sets or returns the
value of the value attribute of a text field.
document.getElementById("result").innerHTM : The innerHTML property sets or
returns the HTML content (inner HTML) of an element.
JavaScript Code :
view plaincopy to clipboardprint?
13. }
14. else
15. {
16. document.write("The values "+ num1+ " and "+num2+ " are equal.");
17. }
www.mrsaem.com 7|Page
www.mrsaem.com 7|Page
A-LEVEL IT
Practice More JavaScript (9626)
JavaScript Code :
view plaincopy to clipboardprint?
1. var x=3;
2. var y=-7;
3. var z=2;
4. if (x>0 && y>0 && z>0)
5. {
6. alert("The sign is +");
7. }
8. else if (x<0 && y<0 && z>0)
9. {
10. document.write("The sign is +");
11. }
12. else if (x>0 && y<0 && z<0)
13. {
14. document.write("The sign is +");
15. }
16. else if (x<0 && y>0 && z<0)
17. {
18. document.write("The sign is +");
19. }
20. else
21. {
22. document.write("The sign is -");
23. }
www.mrsaem.com 8|Page
www.mrsaem.com 8|Page
A-LEVEL IT
Practice More JavaScript (9626)
Sample Solution :
avaScript Code :
view plaincopy to clipboardprint?
1. is_array = function(input) {
2. if (toString.call(input) === "[object Array]")
3. return true;
4. return false;
5. };
6. document.write(is_array('w3resource'));
7. document.write(is_array([1, 2, 4, 0]))
JavaScript Code :
view plaincopy to clipboardprint?
1. array_Clone = function(arra1) {
2. return arra1.slice(0);
3. };
4. document.write(array_Clone([1, 2, 4, 0]));
5. document.write(array_Clone([1, 2, [4, 0]]));
JavaScript Code :
view plaincopy to clipboardprint?
www.mrsaem.com 9|Page
www.mrsaem.com 9|Page
A-LEVEL IT
Practice More JavaScript (9626)
1. var arr1=[-3,8,7,6,5,-4,3,2,1];
2. var arr2=[];
3. var min=arr1[0];
4. var pos;
5. max=arr1[0];
6. for (i=0; i<arr1.length; i++)
7. {
8. if (max<arr1[i]) max=arr1[i];
9. }
10.
11. for (var i=0;i<arr1.length;i++)
12. {
13. for (var j=0;j<arr1.length;j++)
14. {
15. if (arr1[j]!="x")
16. {
17. if (min>arr1[j])
18. {
19. min=arr1[j];
20. pos=j;
21. }
22. }
23. }
24. arr2[i]=min;
25. arr1[pos]="x";
26. min=max;
27. }
28. document.write(arr2);
www.mrsaem.com 10 | P a g e
www.mrsaem.com 10 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
JavaScript Code :
www.mrsaem.com 11 | P a g e
www.mrsaem.com 11 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
Output : 0
Sample Solution:-
1. a=-5;
2. b=-2;
3. c=-6;
4. d= 0;
5. f=-1;
6. if (a>b && a>c && a>d && a>f)
7. {
8. document.write(a);
9. }
10. else if (b>a && b>c && b>d && b>f)
11. {
12. document.write(b);
13. }
14. else if (c>a && c>b && c>d && c>f)
15. {
16. document.write(c);
17. }
18. else if (d>a && d>c && d>b && d>f)
19. {
20. document.write(d);
21. }
22. else
23. {
24. document.write(f);
25. }
JavaScript Code :
view plaincopy to clipboardprint?
1. var x= 0;
2. var y=-1;
3. var z= 4;
4. if (x>y && x>z)
www.mrsaem.com 12 | P a g e
www.mrsaem.com 12 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
5. {
6. if (y>z)
7. {
8. document.write(x + ", " + y + ", " +z);
9. }
10. else
11. {
12. document.write(x + ", " + z + ", " +y);
13. }
14. }
15. else if (y>x && y >z)
16. {
17. if (x>z)
18. {
19. document.write(y + ", " + x + ", " +z);
20. }
21. else
22. {
23. document.write(y + ", " + z + ", " +x);
24. }
25. }
26. else if (z>x && z>y)
27. {
28. if (x>y)
29. {
30. document.write(z + ", " + x + ", " +y);
31. }
32. else
33. {
34. document.write(z + ", " + y + ", " +x);
35. }
36. }
JavaScript Code :
www.mrsaem.com 13 | P a g e
www.mrsaem.com 13 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
JavaScript Code :
view plaincopy to clipboardprint?
www.mrsaem.com 14 | P a g e
www.mrsaem.com 14 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
366
Sample Solution:-
JavaScript Code :
view plaincopy to clipboardprint?
1. function days_passed(dt) {
2. var current = new Date(dt.getTime());
3. var previous = new Date(dt.getFullYear(), 0, 1);
4.
5. return Math.ceil((current - previous + 1) / 86400000);
6. }
7. document.write(days_passed(new Date(2015, 0, 15)));
8. document.write(days_passed(new Date(2015, 11, 14)));
JavaScript Code :
view plaincopy to clipboardprint?
1. function calculate_age(dob) {
2. var diff_ms = Date.now() - dob.getTime();
3. var age_dt = new Date(diff_ms);
4.
5. return Math.abs(age_dt.getUTCFullYear() - 1970);
6. }
7.
8. document.write(calculate_age(new Date(1982, 11, 4)));
9.
10. document.write(calculate_age(new Date(1962, 1, 1)));
www.mrsaem.com 15 | P a g e
www.mrsaem.com 15 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
Test Data :
dt1 = new Date("October 13, 2014 08:11:00");
dt2 = new Date("October 19, 2014 11:13:00");
document.write(diff_days(dt1, dt2));
6
Sample Solution:-
JavaScript Code :
JavaScript Code :
view plaincopy to clipboardprint?
1. function startOfWeek(date)
2. {
3. var diff = date.getDate() - date.getDay() + (date.getDay() === 0 ? -
6 : 1);
4.
5. return new Date(date.setDate(diff));
6.
7. }
8.
9. dt = new Date();
10.
11. document.write(startOfWeek(dt).toString());
www.mrsaem.com 16 | P a g e
www.mrsaem.com 16 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
JavaScript Code :
view plaincopy to clipboardprint?
1. function endOfWeek(date)
2. {
3.
4. var lastday = date.getDate() - (date.getDay() - 1) + 6;
5. return new Date(date.setDate(lastday));
6.
7. }
8.
9. dt = new Date();
10.
11. document.write(endOfWeek(dt).toString());
JavaScript Code :
view plaincopy to clipboardprint?
1. function startOfMonth(date)
2. {
3.
4. return new Date(date.getFullYear(), date.getMonth(), 1);
5.
6. }
7.
8. dt = new Date();
9.
10. document.write(startOfMonth(dt).toString());
www.mrsaem.com 17 | P a g e
www.mrsaem.com 17 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
document.write(is_Blank(''));
document.write(is_Blank('abc'));
true
false
Sample Solution:-
JavaScript Code :
view plaincopy to clipboardprint?
1. is_Blank = function(input) {
2. if (input.length === 0)
3. return true;
4. else
5. return false;
6. }
7. document.write(is_Blank(''));
8. document.write(is_Blank('abc'));
www.mrsaem.com 18 | P a g e
www.mrsaem.com 18 | P a g e
A-LEVEL IT
Practice More JavaScript (9626)
1. //capitalize_Words
2. function capitalize_Words(str)
3. {
4. return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCa
se() + txt.substr(1).toLowerCase();});
5. }
6. document.write(capitalize_Words('js string exercises'));
www.mrsaem.com 19 | P a g e
www.mrsaem.com 19 | P a g e