Guys please help me through this assighnment i would really appreciate it. You w
ID: 3771048 • Letter: G
Question
Guys please help me through this assighnment i would really appreciate it.
You will create a bunch of functions to practice nuking them. Your page should have ONE button that runs a main that I will provide to you. Try to write and test one function at a time before continuing on. Most syntax errors I see crop up when everyone tries to put in all of their code at once. Build small, test often, don't add code until you know what you have is good. Don't forget about the debugger! Write a function phone() that alerts "ring ring" when called. Write a function helloWorld() that alerts "Hello World" when called. Write a function even(num) that returns true if a number is even. It should return false if the number is odd. Write a function rollDie(sides) that returns a random number between 1 and the value of sides. Write a function bigger(num1, num2) that returns the bigger number of the two parameters. Write a function smaller(num1, num2) that returns the smaller number of the two parameters. Write a function lowerCounter(string) that takes a String parameter and returns the number of vowels in a String. Vowels are a, e, i, o, u. Write a function consonantCounter(string) that takes a String parameter and returns the number of non-vowel letters in a String. Write a function upperCounter(string) that takes a String parameter and returns the number of uppercase characters in a String. Write a function lowerCounter(string) that takes a String parameter and returns the number of lowercase characters in a String. Write a function is.Alphabetic(string) that takes a String parameter and returns true if there are only letters (no numbers, no punctuation and no spaces) in the string. Return false If there are non letter characters in the string. Write a function islnteger(string) that takes a String parameter and returns true if there are only numbers in the suing. Return false if it has any letters. Write a function reverseString(string) that takes a String as a parameter and returns a reversed version of a String. Example: "Go" should become "oG" Write a function isPalindrome(string) that takes a String as a parameter and returns true if the String is a palindrome. A palindrome is a word that looks the same written both forward and backwards. Example: "radar" is "radar" if you spell it backward. Write a function phoneWords(string) that lakes a string containing a phone number that could contain letters like "1 -800-Flowers" and returns what you would actually dial on the phone. "1-800-Flowers" would result in 18003569377.Explanation / Answer
<!DOCTYPE html>
<html>
<body>
<p>Click the button to Test all functions.</p>
<button>Click me</button>
<script>
//1
function phone() {
alert('ring ring');
}
//2
function helloWorld() {
alert('Hello World');
}
//3
function even(num) {
return num % 2 == 0;
}
//4
function rollDie(sides) {
return Math.floor(Math.random() * sides);
}
//5
function bigger(num1, num2) {
return (num1 > num2) ? num1 : num2;
}
//6
function smaller(num1, num2) {
return (num1 < num2) ? num1 : num2;
}
//7
function vowelCounter(string) {
return string.match(/[aeiou]/gi).length;
}
//8
function consonantCounter(string) {
var vowel = string.match(/[aeiou ]/gi).length;
return string.length - vowel;
}
//9
function upperCounter(string) {
var count = 0, len = string.length;
for ( var i = 0; i < len; i++) {
if (/[A-Z]/.test(string.charAt(i)))
count++;
}
return count;
}
//10
function lowerCounter(string) {
var count = 0, len = string.length;
for ( var i = 0; i < len; i++) {
if (/[a-z]/.test(string.charAt(i)))
count++;
}
return count;
}
//11
function isAlphabetic(string) {
if (!/[^a-zA-Z]/.test(string))
return true;
else
return false;
}
//12
function isInteger(string) {
if (!/[^0-9]/.test(string))
return true;
else
return false;
}
//13
function reversString(string) {
var reversed = "";
var len = string.length;
for ( var i = 1; i < (len + 1); i++) {
reversed += string[len - i];
}
return reversed;
}
//14
function isPalindrome(string) {
if (string == reversString(string))
return true;
else
return false;
}
function phoneWords(string){
var phoneWord = "";
var len = string.length;
for ( var i = 0; i < len ; i++) {
if(!/[^a-cA-C]/.test(string[i])){
phoneWord +='2';
}else if(string[i]=='-'){
}else if(!/[^d-fD-F]/.test(string[i])){
phoneWord +='3';
}else if(!/[^g-iG-I]/.test(string[i])){
phoneWord +='4';
} else if(!/[^j-lJ-L]/.test(string[i])){
phoneWord +='5';
} else if(!/[^m-oM-O]/.test(string[i])){
phoneWord +='6';
} else if(!/[^p-sP-S]/.test(string[i])){
phoneWord +='7';
} else if(!/[^t-vT-V]/.test(string[i])){
phoneWord +='8';
} else if(!/[^w-zW-Z]/.test(string[i])){
phoneWord +='9';
} else{
phoneWord +=string[i];
}
}
return phoneWord;
}
function testFunctions() {
phone();
helloWorld();
alert('even(10)-->'+even(10));
alert('rollDie(10)-->'+rollDie(10));
alert('bigger(10, 20)-->'+bigger(10, 20));
alert('smaller(10, 20)-->'+smaller(10, 20));
alert('vowelCounter(srinivas)-->'+vowelCounter('srinivas'));
alert('consonantCounter(srinivas)-->'+consonantCounter('srinivas'));
alert('upperCounter(SrIniVas)-->'+upperCounter('SrIniVas'));
alert('lowerCounter(SrIniVas)-->'+lowerCounter('SrIniVas'));
alert('isAlphabetic(srinivas)-->'+isAlphabetic('srinivas'));
alert('isAlphabetic(srinivas2)-->'+isAlphabetic('srinivas2'));
alert('isInteger(srinivas)-->'+isInteger('srinivas'));
alert('isInteger(234)-->'+isInteger('234'));
alert('reversString(srinivas)-->'+reversString('srinivas'));
alert('isPalindrome(radar)-->'+isPalindrome('radar'));
alert('phoneWords(1-800-Flower)-->'+phoneWords('1-800-Flower'));
}
</script>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.