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