SHARE
TWEET

Untitled

a guest Feb 18th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Code your transform function here:
  2. function transform(obj){
  3.   let key = '';
  4.   let newObj= {};
  5.   //loop through obeject keys
  6.   for(let item in obj){
  7.     for(let letter in obj[item]){
  8.       newObj[obj[item][letter].toLowerCase()] = Number(item);
  9.      }
  10.   }
  11.   return newObj;
  12. }
  13.  
  14.  
  15. // Code your initialPrompt function here:
  16. const input = require('readline-sync');
  17.  
  18. let initialPrompt = input.question("Welcome to the Scrabble score calculator! \n \nWhich scoring algorithm would you like to use? \n \n0 - Scrabble: The traditional scoring algorithm. \n1 - Simple Score: Each letter is worth 1 point. \n2 - Bonus Vowels: Vowels are worth 3 pts, and consonants are 1 pt. \n \nEnter 0, 1, or 2:");
  19. while(initialPrompt > 2){
  20.   initialPrompt = input.questionint("Please enter 0, 1, or 3");
  21. }
  22.  
  23. // Code your runProgram function here:
  24. function runProgram(arr){
  25.   console.log("Using algorithm: ", scoringAlgorithms[initialPrompt].name);
  26.   let word = input.question("Enter a word to be scored, or 'Stop' to quit:");
  27.   if(word !== 'Stop'){
  28.     console.log(`Score for ${word}: `, scoringAlgorithms[initialPrompt].scoreFunction(word));
  29.     word = '\n';
  30.     runProgram();
  31.   }
  32. }
  33.  
  34.  
  35. // Here is the oldScoreKey object:
  36. const oldScoreKey = {
  37.    1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
  38.    2: ['D', 'G'],
  39.    3: ['B', 'C', 'M', 'P'],
  40.    4: ['F', 'H', 'V', 'W', 'Y'],
  41.    5: ['K'],
  42.    8: ['J', 'X'],
  43.    10: ['Q', 'Z']
  44. };
  45.  
  46.  
  47.  
  48. // Use the transform function to create the newScoreKey object here:
  49. let newScoreKey = transform(oldScoreKey);
  50.  
  51. // Create your scoringAlgorithms array here:
  52.  
  53. let scoringAlgorithms = [
  54. {name: 'Scrabble',
  55. description: 'The traditional scoring algorithm',
  56. scoreFunction: function scrabbleScore(newScoreKey,word){
  57.   let score = 0;
  58.   for(let letters of word.toLowerCase()){{
  59.       score += newScoreKey[letters];
  60.     }
  61.   }
  62.   return score;
  63. }
  64. },
  65. {name: 'Simple Score',
  66. description: 'Each letter is worth 1 point',
  67. scoreFunction: function simpleScore(word){
  68.   let score = word.length;
  69.   return score;
  70. }
  71. },
  72. {name: 'Bonus Vowels',
  73. description: 'Vowels are 3 pts. consonants are 1 pt.',
  74. scoreFunction: function bonusVowels(word){
  75.   let vowels = ['a','e', 'i', 'o', 'u'];
  76.   let score = word.length;
  77.   for(let i = 0; i < word.length; i++){
  78.     if(vowels.includes(word[i].toLowerCase())){
  79.       score += 2;
  80.     }
  81.   }
  82.  return score;
  83. }
  84. }];
  85.  
  86.  
  87.  
  88. //console.log("algorithm name: ", scoringAlgorithms[0].name);
  89. //console.log("scoreFunction result: ", scoringAlgorithms[0].scoreFunction(newScoreKey, "JavaScript"));
  90.  
  91.  
  92. //console.log(scrabbleScore(newScoreKey, 'hello'));
  93. //console.log(bonusVowels("HELLO"));
  94. //console.log(simpleScore('hello'))
  95. // Call the runProgram function here:
  96.  
  97. runProgram(scoringAlgorithms)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top