Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЕ С IF-ELSE:
- function vowelsSum(input) {
- let word = input[0];
- let sum = 0;
- for (let i = 0; i < word.length; i++) {
- if (word[i] === 'a') {
- sum += 1;
- } else if (word[i] === 'e') {
- sum += 2;
- } else if (word[i] === 'i') {
- sum += 3;
- } else if (word[i] === 'o') {
- sum += 4;
- } else if (word[i] === 'u') {
- sum += 5;
- }
- }
- console.log(sum);
- }
- РЕШЕНИЕ СЪС SWITCH:
- function vowelsSum(input) {
- let word = input[0];
- let sum = 0;
- for (let i = 0; i < word.length; i++) {
- switch (word[i]) {
- case 'a':
- sum += 1;
- break;
- case 'e':
- sum += 2;
- break;
- case 'i':
- sum += 3;
- break;
- case 'o':
- sum += 4;
- break;
- case 'u':
- sum += 5;
- break;
- }
- }
- console.log(sum);
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- function vowelsSum(input) {
- let word = input[0];
- let sum = 0;
- for (let i = 0; i < word.length; i++) {
- sum +=
- word[i] === 'a' ? 1 :
- word[i] === 'e' ? 2 :
- word[i] === 'i' ? 3 :
- word[i] === 'o' ? 4 :
- word[i] === 'u' ? 5 : 0;
- }
- console.log(sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement