-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter08.js
75 lines (53 loc) · 1.77 KB
/
chapter08.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
## Coding time!
### Word info
Write a program that asks you for a word then shows its length, lowercase, and uppercase values.
*/
function wordInfo() {
const word = prompt("Please enter a word");
const length = word.length;
const lowercase = word.toLowerCase();
const uppercase = word.toUpperCase();
alert(`The word ${word} has ${length} characters, in lowercase it is ${lowercase} and in uppercase it is ${uppercase}`);
}
/*
### Vowel count
Improve the previous program so that it also shows the number of vowels inside the word.
*/
function vowelCount () {
const word = prompt("Please enter a word");
const vowels = ["a", "e", "i", "o", "u"];
let count = 0;
for (let i = 0; i < word.length; i++) {
if (vowels.includes(word[i])) {
count++;
}
}
alert(`The word ${word} has ${count} vowels`);
}
/*
### Backwards word
Improve the previous program so that it shows the word written backwards.
*/
function reversedWord() {
const word = prompt("Please enter a word");
const reversed = word.split("").reverse().join("");
alert(`The word ${word} written backwards is ${reversed}`);
}
/*
### Palindrome
Improve the previous program to check if the word is a palindrome. A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
For example, `"radar"` should be detected as a palindrome, `"Radar"` too.
*/
function palindrome(word) {
const lowercaseWord = word.toLowerCase();
const reversed = lowercaseWord.split("").reverse().join("");
return lowercaseWord === reversed;
}
const word = prompt("Please enter a word");
const isPalindrome = palindrome(word);
if (isPalindrome) {
console.log(`The word ${word} is a palindrome`);
} else {
console.log(`The word ${word} is not a palindrome`);
}