Jump to content

Very new to JavaScript and trying to understand Functions! Can some please help...


Recommended Posts

Hi there, I've recently started to try and learn JavaScript on my own in my spare time, as it's an area of work I'd like to get into (not exactly loving my current occupation). I'm trying to learn it from a book I got and finding it very difficult! Much harder than html and css (which I've been learning for a few months as well). I'm trying to answer this question below with a 'for loop' (which is partially running), but I'm really lost as to how to answer it using a function?? Could anyone give me some advice, as it's really doing my head in! It'd be greatly appreciated indeed....

Question:

" Write a function that takes a character and returns false if it is not a vowel and true if it is. This function should be used in a program to count the number of non-vowels (not just consonants) and the number of vowels in a string that the user is asked to input.
E.g. JavaScript Rocks!
Number of vowels: 4
Number of non-vowels: 13 "


Here is my attempt so far.....


<html>
<body><title>Function</title></head>
<body>
<h1>Function</h1>
<script>

var sent = (prompt("Please write a short sentence of your choosing"));
var vowels = 0;
var nonevowels = 0;

for (i=0;i<sent.length; i++) {
if(sent == "a" || sent == "e" || sent == "i" || sent == "o" || sent == "u") {
vowels++;
}
if(sent == "A" || sent == "E" || sent == "I" || sent == "O" || sent == "U") {
vowels++;
}
else { nonevowels++;
}
}
document.write("Numberowels = " + vowels + "\n");
document.write("Numberone vowels = " + nonevowels);

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

Link to post
Share on other sites

Javascript is more difficult than HTML and CSS because it's a real programming language.

 

You haven't created a function. The first three words of the problem are "Write a function". You should first learn how to make a function here: http://www.w3schools.com/js/js_functions.asp

Link to post
Share on other sites

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Function</title>

<script>

window.onerror = function(a,b,c){

alert('Javascript Error: '+a+'\nURL: '+b+'\nLine Number: '+c);

return true;

}

</script>

</head>

 

<body>

 

<h1>Function</h1>

<input type="text" id="intxt"/>

<input type="button" id="btn" value="Run"/>

<div id="outtxt">

</div>

 

<script>

'use strict';

document.getElementById('btn').onclick = getinputtxt;

 

function getinputtxt(){

var outtxt = document.getElementById('outtxt');

outtxt.innerHTML = '';

var t = document.getElementById('intxt').value;

var o = vowels(t);

outtxt.innerHTML = o;

}

 

function vowels(raw){

var vowels = 0;

var nonvowels = 0;

var sent = raw.trim().toUpperCase();

 

for (var i=0 ; i<sent.length ; i++) {

if(sent[i] == ' '){

// do nothing

}else if(sent[i] == "A" || sent[i] == "E" || sent[i] == "I" || sent[i] == "O" || sent[i] == "U") {

vowels++;

}else{

nonvowels++;

}

}

var str = "Number vowels = " + vowels + "<br/>Number non-vowels = " + nonvowels;

return str;

}

</script>

</body>

</html>

Link to post
Share on other sites

My code does not really satisfy the problem requirements stated in the original post, but it does demonstrate the use of a function.

 

If you want to have a function that returns a boolean you might do something like this...

function vowel(ch){
  'use strict';
  if (typeof ch != 'string' || ch.length != 1){
    console.log('Internal error in vowel');
  }
  var chu = ch.toUpperCase();
  if(chu == "A" || chu == "E" || chu == "I" || chu == "O" || chu == "U") {
    return true;
  }else{
    return false;
  }
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...