4

Hey everyone i'm a newbie so please be nice. I've done a search and found stuff related to python but none to JS.

I'm half way through my code academy course on JS and am trying to apply what I've learnt so far in creating a simple numerology script. So far i've learnt functions, loops, variables, and array positions so i imagined this task should be achievable.

The aim is for the user to input their name and the script automatically calculate the value of it according to numerology rules.

For example:

A:1, J:1, S:1, B:2, K:2, T:2, C:3, L:3, U:3, D:4, M:4, V:4, E:5, N:5, W:5, F:6, O:6, X:6, G:7, P:7, Y:7, H:8, Q:8, Z:8, I:9, R:9

So the name TOM would equal 12 because of (2+6+4).

I haven't gotten very far regarding creating a working script and don't really know how to proceed. So far I'm able to isolate the first character in the name but I don't know how to convert it to a number, or how to do the same with the remaining characters:

var x = {A:1, J:1, S:1, B:2, K:2, T:2, C:3, L:3, U:3, D:4, 
M:4, V:4, E:5, N:5, W:5, F:6, O:6, X:6, G:7, P:7, Y:7, H:8, 
Q:8, Z:8, I:9, R:9};

name = prompt ("Type your name in CAPS"); //for example: TOM
name = name[0]; // first letter is T
console.log(name[0]); // prints T
console.log(x.T) // prints 2


console.log(x.(name[0])); //not working. I thought it would also print 2??
TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

4

Please try following code. Here's explanation of what I have done:

  1. First you need to assign the inputted name in a variable ( You should always use var keyword while declaring new variables unless you intentionally want to make it global )
  2. Once you have the name, you need to loop through each character in the name. You can use a for loop for this, and length property of String as the loop limiting condition.
  3. Now, in each iteration of the for loop, you get one character of the name string. You can use this character to get the value for the matching key, using x[ currentCharacter ] e.g. x["T"].
  4. You need to keep a counter variable to store the sum of all character values, so I have declared the nameScore variable before the loop and assigned it an initial value of 0. Each iteration gets the corresponding value for the current character and adds the numeric value to nameScore. By the time the loop finishes, nameScore contains the sum of all character values in the name string. You can console.log() or alert() it, or use in any other way you want.

Second part: Getting single digit value of the nameScore For this part, I followed following steps ( there may be other approaches possible ):

  1. First condition we need to check is that nameScore is greater than 10, otherwise it's already single digit. This check also helps if we the sum of digits is again more than a single digit and we need to calculate the single digit sum again. I have used while loop for this.

  2. In the while loop, we need to again keep a counter( initialised at 0 ), iterate through each digit and add it to the counter. To loop through it, we first convert it to a string ( using ''+singleDigitScore) so that we can access length property and use charAt() to get individual digits. As charAt() returns a string representation of the digit, we use parseInt() to convert it to number before adding it to our counter. If the resulting number is less than 10, the loop is exited, otherwise the same loop runs again with number updated to the new number just calculated.

    var x = {A:1, J:1, S:1, B:2, K:2, T:2, C:3, L:3, U:3, D:4, 
        M:4, V:4, E:5, N:5, W:5, F:6, O:6, X:6, G:7, P:7, Y:7, H:8, 
        Q:8, Z:8, I:9, R:9};
        
        var name = prompt ("Type your name in CAPS"); //for example: TOM
        var nameScore = 0;
        
        for( var i = 0; i < name.length; i++ )
        {
         var curChar = name.charAt( i );
         var curValue = x[ curChar ];
         nameScore = nameScore + curValue;
        }//for()
        
        
        console.log( "Total score for this name is: " + nameScore );
    
    var singleDigitScore = nameScore;
    while( singleDigitScore >= 10 )
      {
        var total = 0;
        var str = '' + singleDigitScore;
        for( var i = 0; i < str.length; i++ )
          {
            total = total + parseInt( str.charAt(i) );
          }//for()
        singleDigitScore = total;
      }//while
    
    console.log("Single Digit score is: "+singleDigitScore);
Community
  • 1
  • 1
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
1

Totally appreciate the answer by Mohit Bhardwaj, and the explaination behind it also for the questioner who want to learn the basic understanding of JavaScript. so once you are good in that and you know how to iterate through a string, then to learn other javascript utilities and concepts you can still try it in different ways. like slit it to learn how split works and the run map or reduce in array, and then use forEach to learn it, here I am giving a quick example to solve this so you can learn other codes in JavaScript too

var name = ..... // first get value in name variable however you want
var sum = name.split('').reduce(function(total, eachNumKey){
return (isNaN(total) ? x[total] : total) + x[eachNumKey]
});

Here I am splitting the string into a array, so that I am running a forEach instead of maintain index and fetch by index, then running reduce to calculate sum, (could have map the array first to transform equivalent value array then easily you can sum too, but then will be i iteration of 2 times, but you should learn map also in some other cases). In reduce for the first time it the first arg is the first value of array, and second is the second , but next time onward first argument will be whatever you returned, and second argument will be the iterator, so just checked with a condition, and mapped to corresponding value need to add. Note: Performance wise this is nit the better solution for this particular problem, but if you try every thing with different approaches then you will get to know their uses and when to try what.

Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32