fintanstrasbourg Posted December 2, 2016 Share Posted December 2, 2016 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: 4Number 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 comment Share on other sites More sharing options...
Ingolme Posted December 2, 2016 Share Posted December 2, 2016 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 comment Share on other sites More sharing options...
davej Posted December 2, 2016 Share Posted December 2, 2016 <!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 comment Share on other sites More sharing options...
Ingolme Posted December 2, 2016 Share Posted December 2, 2016 People can't learn if you just give them the answer. 1 Link to comment Share on other sites More sharing options...
fintanstrasbourg Posted December 3, 2016 Author Share Posted December 3, 2016 Hi guys, thank you both very much for the help and advice on how to go about solving the problem, it's helped me a lot! Much appreciated indeed Link to comment Share on other sites More sharing options...
davej Posted December 4, 2016 Share Posted December 4, 2016 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 comment Share on other sites More sharing options...
dsonesuk Posted December 4, 2016 Share Posted December 4, 2016 OK! You don't have to sit on naughty step, you are forgiving. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now