# Thread: Ten Simple Javascript Exercises

1. Registered User
Join Date
Dec 2009
Posts
1

## my solution,sorry i'm just a beginner

I'm just a beginner,so regarding my solution to question 1,i've no idea whether it's correct or wrong,but hope any expert can correct and teach me(i just learn javascript last 2 weeks)

<!-- 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.
-->

<html>
<body>

<script type="text/javascript">

function max(firstNumber , secondNumber)
{
if (firstNumber > secondNumber)
{
document.write(firstNumber);
}
else if (isNaN(firstNumber) || isNaN(secondNumber))
{
document.write("You are not entering a number");
}
else
{
document.write(secondNumber);
}
}
var firstNumber = parseFloat(prompt("Please enter your first number","")); <!-- the function parseFloat is use to prevent error when comparing negative number -->
max(firstNumber, secondNumber);

</script>
</body>
</html>

2. Registered User
Join Date
May 2011
Posts
8
I know this is a REALLY old thread but Im going to add to it anyway. Attempting the OP's homework for him to teach myself and ran into a problem. The following code is the working solution to questions 1 & 2:

------------------------------------------------------
<!--
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.
-->

<script type = "text/javascript">

var num1 = 8;
var num2 = 40;
var num3 = 76;

var maxNum = Math.max(num1, num2, num3);

if (maxNum == num1) {
}
else if (maxNum == num2) {
}
else {
}

<body>

<p id = "maxField">&nbsp;</p>

</body></html>
-----------------------------------------

This works fine but when I use the following code in place of the 3 alert(); in the if...else statement Firebug warns me that maxLoc is null.
var maxLoc = document.getElementById("maxField");
maxLoc.innerHTML = num1;

it is not a syntax error because it runs fine if the if statement points to an alert(). I used the .innerHTML exaclty the same way before so Im confused. Any help?

3. Originally Posted by JohnnyCK
it is not a syntax error
It is a syntax error, because the code runs before the element with the id="maxField" is loaded, so there is no element to be found at that time. Place the code within a function and call it onload
Code:
```<script type = "text/javascript">
function myFunction(){
// the code here
}
</script>```

4. Registered User
Join Date
May 2011
Posts
8
Works now. Thank You

5. Registered User
Join Date
May 2011
Posts
8
Working on another question now. Trying to simply print all the elements in an array.

var vowelArray = new Array('a', 'e', 'i', 'o', 'u');

for ( i=0; i < vowelArray.length; i++ ) {
var createdVowelString = createdVowelString + vowelArray[i];
}
var vowelLoc = document.getElementById("vowelField");
vowelLoc.innerHTML = createdVowelString;

this works but prints undefined before the array. So "undefinedaeiou". I think undefined is because createdVowelString is originally undefined but if I assign it a value to begin with the loop only prints out the last letter in array 'u'. Like this:

var createdVowelString = "";
createdVowelString = createdVowelString + vowelArray[i];

6. Registered User
Join Date
May 2011
Posts
8
Just found array.toString is a much easier solution to my problem but if anyone sees why I got undefined I would still like to know so I understand better.

7. Registered User
Join Date
Sep 2013
Posts
1
I know OP had questionable motives, but I solved these questions. If anyone is interested in the answer,

function max(x,y){
if(x > y){
return x;
}
else{
return y;
}
}
function max_of_three(x,y,z){
var threes_company = [x,y,z];
threes_company.sort();
var howbig = threes_company.length;
return threes_company[howbig - 1];
}
function vowel(c){
var vowels = ['a','e','i','o','u'];
if (vowels.indexOf(c) > -1){
return true;
}
return false;
}
function translate(word){
var translation = "";
for (var x in word ){
if(!vowel(word[x])){
translation += word[x] + 'o' + word[x]
}
else{
translation += word[x];
}
}
return translation;
}
function sum(a){
var the_sum = 0;
for (var x in a){
the_sum += a[x];
}
return the_sum;
}
function multiply(a){
var the_product = 1;
for (var x in a){
the_product *= a[x];
}
return the_product;
}
function reverse(s){
for(i = s.length - 1; i >= 0; i--){
document.write(s[i]);
}
}
function sort_alpha(a){
a.sort();
for (var x in a){
document.write(a[x] + ' ');
}
}
function findLongestWord(a){
var longest = 0;
for (var x in a){
if (a[x].length > longest){
longest = a[x].length;
}
}
return longest;
}
function filterLongWords(a,i){
var long_array = [];
for (var x in a){
if (a[x].length > i){
long_array.push(a[x]);
}
}
return long_array;
}
function charFreq(s){
var frequencies = {};
for (var x in s){
if (typeof frequencies[s[x]] === 'undefined'){
frequencies[s[x]] = 1;
} else{
frequencies[s[x]] += 1;
}
}
return frequencies;
}

8. Registered User
Join Date
Aug 2013
Posts
3
Here is an easier way;

var num1 = prompt("Enter the first number: ", " ");
var num2 = prompt("Enter the second number: ", " ");

function max(){
if(num1 > num2){
document.write("The first number is the largest");
}
else{
document.write("The second number is the largest");
}
}
max();

9. Registered User
Join Date
Aug 2013
Posts
3
I thought this way was easier:

var str = prompt("Enter a character: ", " ");
var myVowels = ["a", "e", "i", "o", "u"];

function testVowels(){
if(str == myVowels[0]){
if(str == myVowels[1]){
if(str == myVowels[2]){
if(str == myVowels[3]){
if(str == myVowels[4]){
}
}
}
}
document.write("True");
console.log("True");
console.log(str.length);
}
else{
document.write("False");
console.log("False");
console.log(str.length);
}
}
testVowels();

10. Registered User
Join Date
Aug 2013
Posts
3
Here is the second exercise:

var num1 = prompt("Enter the first number: ", " ");
var num2 = prompt("Enter the second number: ", " ");
var num3 = prompt("Enter the third number: ", " ");

function maxOfThree(){
document.write("The largest number is: ", (Math.max(num1, num2, num3)));
}
maxOfThree();

11. Registered User
Join Date
Apr 2014
Location
Dubai UAE
Posts
1
Hi gays.........
naw i just want to know because i want to learn stuff .

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11826 seconds
• Memory Usage 2,964KB
• Queries Executed 15 (?)
Template Usage (37):
• (1)bbcode_code
• (2)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (11)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (1)pagenav
• (1)pagenav_curpage
• (11)postbit
• (11)postbit_onlinestatus
• (11)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (74):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• pagenav_page
• pagenav_complete
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete