Adding five code won't print!!!

+1 Hassan Alzaher · April 6, 2015
Hi everyone, I'm having problem figuring out this code.
This code supposes to print numbers like

tNumsPlus5(3463487) => '8918932'
tNumsPlus5(5418675309) => '0963120854'

It should add 5 to each digit


var tNumsPlus5 = function(tnum){
    "use strict";
    //declare local variables
    var result = "";
    //convert tunm to a String
    tnum = String(tnum);
    //traverse string and process digits
    for (var i = 0; i < tnum.length; i++){
        //process the current digit
        digit = (tnum[i] + 5) % 10;
        console.log(digit);
    }
    
    return result;
    
    
};

Post a Reply

Replies

Oldest  Newest  Rating
0 Jay Tauron · April 7, 2015
var tNumsPlus5 = function(tnum){
    "use strict";
    //declare local variables
    var result = "";
    //convert tunm to a String
    tnum = ''+tnum;
    //traverse string and process digits
    for (var i = 0; i < tnum.length; i++){
        //process the current digit
        result += (+tnum[i] + 5) % 10; // +tnum[i] turns the string to a number before adding 5
// before, tnum[i]+5 just appended 5 as a string i.e. '2' + 5 = '25'
    }
    
    return result;
    
    
};
0 Hassan Alzaher · April 10, 2015
Thank you Jay for your help :)
  • 1

Javascript

107,345 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators