Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code your transform function here:
- function transform(obj){
- let key = '';
- let newObj= {};
- //loop through obeject keys
- for(let item in obj){
- for(let letter in obj[item]){
- newObj[obj[item][letter].toLowerCase()] = Number(item);
- }
- }
- return newObj;
- }
- // Code your initialPrompt function here:
- const input = require('readline-sync');
- 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:");
- while(initialPrompt > 2){
- initialPrompt = input.questionint("Please enter 0, 1, or 3");
- }
- // Code your runProgram function here:
- function runProgram(arr){
- console.log("Using algorithm: ", scoringAlgorithms[initialPrompt].name);
- let word = input.question("Enter a word to be scored, or 'Stop' to quit:");
- if(word !== 'Stop'){
- console.log(`Score for ${word}: `, scoringAlgorithms[initialPrompt].scoreFunction(word));
- word = '\n';
- runProgram();
- }
- }
- // Here is the oldScoreKey object:
- const oldScoreKey = {
- 1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
- 2: ['D', 'G'],
- 3: ['B', 'C', 'M', 'P'],
- 4: ['F', 'H', 'V', 'W', 'Y'],
- 5: ['K'],
- 8: ['J', 'X'],
- 10: ['Q', 'Z']
- };
- // Use the transform function to create the newScoreKey object here:
- let newScoreKey = transform(oldScoreKey);
- // Create your scoringAlgorithms array here:
- let scoringAlgorithms = [
- {name: 'Scrabble',
- description: 'The traditional scoring algorithm',
- scoreFunction: function scrabbleScore(newScoreKey,word){
- let score = 0;
- for(let letters of word.toLowerCase()){{
- score += newScoreKey[letters];
- }
- }
- return score;
- }
- },
- {name: 'Simple Score',
- description: 'Each letter is worth 1 point',
- scoreFunction: function simpleScore(word){
- let score = word.length;
- return score;
- }
- },
- {name: 'Bonus Vowels',
- description: 'Vowels are 3 pts. consonants are 1 pt.',
- scoreFunction: function bonusVowels(word){
- let vowels = ['a','e', 'i', 'o', 'u'];
- let score = word.length;
- for(let i = 0; i < word.length; i++){
- if(vowels.includes(word[i].toLowerCase())){
- score += 2;
- }
- }
- return score;
- }
- }];
- //console.log("algorithm name: ", scoringAlgorithms[0].name);
- //console.log("scoreFunction result: ", scoringAlgorithms[0].scoreFunction(newScoreKey, "JavaScript"));
- //console.log(scrabbleScore(newScoreKey, 'hello'));
- //console.log(bonusVowels("HELLO"));
- //console.log(simpleScore('hello'))
- // Call the runProgram function here:
- runProgram(scoringAlgorithms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement