Ten Simple Javascript Exercises

Show 40 post(s) from this thread on one page
Page 4 of 4 First ... 234
• 12-10-2009, 08:13 AM
caramel1991
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)

Quote:

<!-- 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>
• 05-16-2011, 12:00 AM
JohnnyCK
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?
• 05-16-2011, 04:28 AM
Kor
Quote:

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 } onload=myFunction; </script>```
• 05-16-2011, 12:54 PM
JohnnyCK
Works now. Thank You
• 05-16-2011, 02:16 PM
JohnnyCK
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];
• 05-16-2011, 02:27 PM
JohnnyCK
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.
• 09-09-2013, 03:43 PM
u0392185
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;
}
• 04-01-2014, 11:25 PM
sidney123
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();
• 04-01-2014, 11:30 PM
sidney123
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();
• 04-01-2014, 11:39 PM
sidney123
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();
• 04-02-2014, 12:52 AM
Danedison
Hi gays.........
naw i just want to know because i want to learn stuff .
Show 40 post(s) from this thread on one page
Page 4 of 4 First ... 234

X vBulletin 4.2.2 Debug Information

• Page Generation 0.31505 seconds
• Memory Usage 2,391KB
• Queries Executed 11 (?)
Template Usage (24):
• (1)bbcode_code_printable
• (2)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)pagenav
• (1)pagenav_curpage
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./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/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (43):
• init_startup
• 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_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• friendlyurl_resolve_class
• friendlyurl_clean_fragment
• friendlyurl_geturl
• pagenav_page
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates