Here's my code:
function sumDigits(num) {
var string = num.toString();
var result = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] === '-') {
var negative = Number(string[i + 1]) * -1
result = result + negative
} else {
result = result + Number(string[i])
}
}
return result;
}
console.log(sumDigits('-316'))
When I add -316 as an argument, my output is 7. Why is it not taking -3 into the equation? As an FYI, I am still new to coding, and not sure this is even the correct route to take. All input is appreciated.
Learn to debug, console.log and debugger are your friend.
function sumDigits(num) {
var string = num.toString();
var result = 0;
console.log("my string:", string);
for (let i = 0; i < string.length; i++) {
console.log("in loop", i, string[i]);
if (string[i] === '-') {
var negative = Number(string[i + 1]) * -1
console.log("in if ", result, "+", negative);
result = result + negative
console.log("updated result", result);
} else {
console.log("in else ", result, "+", Number(string[i]));
result = result + Number(string[i])
console.log("updated result", result);
}
}
console.log("final result", result);
return result;
}
sumDigits(-317);
Looking at the results you can see you find a negative and you get the next number. On the next iteration you read that same number again. So -3 + 3 is zero. So you cancel it out. You need to skip your loop ahead by one.
function sumDigits(num) {
var string = num.toString();
var result = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] === '-') {
var negative = Number(string[i + 1]) * -1
result = result + negative
// bump i
i++;
} else {
result = result + Number(string[i])
}
}
return result;
}
console.log(sumDigits(-317));
I am checking whether the number is greater than 0 or less than zero.
Then if the number is less than zero, then I am merging the first element of array which will be (-digit) digits[0] = digits[0] + digits[1];.
-then I am removing 2nd element from digits array. digits.splice(1, 1);
And At the end I am lopping through all the digits using forEach loop and returning total of the dum digits to total.
const digitsSum = (number) => {
let total = 0;
if (number < 0) {
let digits = number.toString().split('');
digits[0] = digits[0] + digits[1];
digits.splice(1, 1);
digits.forEach(digit => total += parseInt(digit));
console.log(total);
return total;
} else {
let digits = number.toString().split('');
digits.forEach(digit => total += parseInt(digit));
console.log(total);
return total;
}
}
function calculateDigitSumLogic(number) {
}
digitsSum(-1203);
digitsSum(1203);
digitsSum(-201);
Related
I am trying to count number of combinations in given range and length of combinations. Here is following code.
function generateCombination() {
const perm = [];
const permLength = 2;
const numberRange = 8;
let total = 0;
const getCombinations = (result, permLength, numberRange) => {
if (result.length === permLength) {
//console.log('result: ', result);
return 1;
}
for (var i = 0; i < numberRange; i++) {
if (result.indexOf(i) === -1) {
result.push(i);
total = total + getCombinations(result, permLength, numberRange);
result.pop();
}
}
return 0;
}
getCombinations(perm, permLength, numberRange);
console.log("total: ", total); // expected value "total: 56"
}
generateCombination();
the console log for total variable always print 0. but following code works as expected with little change in for loop code. I couldn't understand how the prefix is works here (somex = somex + fn()). Can someone please help here?
// working solution
function generateCombination() {
const perm = [];
const permLength = 2;
const numberRange = 8;
let total = 0;
const getCombinations = (result, permLength, numberRange) => {
if (result.length === permLength) {
//console.log('result: ', result);
return 1;
}
for (var i = 0; i < numberRange; i++) {
if (result.indexOf(i) === -1) {
result.push(i);
if (getCombinations(result, permLength, numberRange)) {
total += 1;
}
result.pop();
}
}
return 0;
}
getCombinations(perm, permLength, numberRange);
console.log("total: ", total); // expected value is "total: 56" and working here
}
generateCombination();
My question is, I don't understand, why solution 1 (top one) is not working as expected (printing total as 0 rather than 56 )?
Thanks
You could move total into getCombinations and return this value on exit.
function generateCombination() {
const perm = [];
const permLength = 2;
const numberRange = 8;
const getCombinations = (result, permLength, numberRange) => {
if (result.length === permLength) return 1;
let total = 0;
for (let i = 0; i < numberRange; i++) {
if (result.indexOf(i) === -1) {
result.push(i);
total += getCombinations(result, permLength, numberRange);
result.pop();
}
}
return total;
}
console.log("total: ", getCombinations(perm, permLength, numberRange)); // expected value "total: 56"
}
generateCombination();
i think i have wirtten the correct code for the problem only one thing and it that i return the first longest sequence how can i alter that to return the last maximum sequence?
an example from codewars editor :
for input '00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz'
Expected: ['c', 19], instead got: ['0', 19]
here is my code:
function longestRepetition(s) {
var count = 0;
var temp = s.charAt(0);
var arr = [];
for (var i = 0; i < s.length; i++) {
if (temp === s.charAt(i)) {
count++
temp = s.charAt(i)
}
else {
temp = s.charAt(i);
arr.push(count)
count = 1;
}
if(i==s.length-1)
arr.push(count);
}
if(arr.length>0)
{
var Max=arr[0]
for(var i=0;i<arr.length;i++)
{
if(Max<=arr[i])
Max=arr[i];
}
}
else var Max=0;
var mindex=arr.indexOf(Max);
return [s.charAt(mindex),Max]
}
I think this would be easier with a regular expression. Match any character, then backreference that character as many times as you can.
Then, you'll have an array of all the sequential sequences, eg ['000', 'aaaaa']. Map each string to its length and pass into Math.max, and you'll know how long the longest sequence is.
Lastly, filter the sequences by those which have that much length, and return the last item in the filtered array:
function longestRepetition(s) {
const repeatedChars = s.match(/(.)\1*/g);
const longestLength = Math.max(...repeatedChars.map(str => str.length));
const longestChars = repeatedChars.filter(str => str.length === longestLength);
return [longestChars.pop(), longestLength];
}
console.log(longestRepetition('00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz'));
The issue in your code is that minindex is an index in your arr, but that index has nothing to do with s. So s.charAt(minindex) makes no sense. You should maintain for which character you had found the count. For instance you could push in arr both the count and the corresponding character (as a subarray with two values). Then the rest of your code would only need little modification to make it work.
Applying this idea to your code without changing anything else, we get this:
function longestRepetition(s) {
var count = 0;
var temp = s.charAt(0);
var arr = [];
for (var i = 0; i < s.length; i++) {
if (temp === s.charAt(i)) {
count++
temp = s.charAt(i) // Not necessary: was already equal
}
else {
arr.push([temp, count]); // <--- pair, BEFORE changing temp
temp = s.charAt(i);
count = 1;
}
if(i==s.length-1)
arr.push([temp, count]); // <---
}
if(arr.length>0)
{
var Max=arr[0]; // <-- Max is now a pair of char & count
for(var i=0;i<arr.length;i++)
{
if(Max[1]<arr[i][1]) // Comparison changed to just less-than
Max=arr[i];
}
}
else Max=[null, 0]; // Must be a pair here also
return Max; // Just return the pair
}
console.log(longestRepetition('00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz'));
But you can do the same with less code:
function longestRepetition(s) {
let result = [null, 0]; // pair of character and count
for (var i = 0; i < s.length; null) {
let start = i++;
while (i < s.length && s[i] === s[start]) i++; // Find end of series
if (i - start > result[1]) result = [s[start], i - start];
}
return result;
}
console.log(longestRepetition('00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz'));
The solution below answers the question with O(n) runtime:
function longestRepetition(s) {
let count = s.length > 0 ? 1 : 0
let char = s.length > 0 ? s[0] : ''
for (let string_i = 0; string_i < s.length - 1; string_i += 1) {
// keep track of current_char
let current_char = s[string_i]
let next_char = s[string_i + 1]
// while the next char is same as current_char
let tracker = 1
while (current_char === next_char) {
// add one to tracker
tracker += 1
string_i += 1
next_char = s[string_i + 1]
}
// if tracker greater than count
if (tracker > count) {
// returned char = current char
// count =tracker
count = tracker;
char = current_char;
}
}
return [char, count]
}
console.log(longestRepetition("bbbaaabaaaa"))//, ["a",4]
I need to build a function that returns a given number shuffled writing one digit from the front of the number and the following taken from the back, then the 3rd digit from the front of the number and the 4th from the back and so on.
Example:
const initialNumber = 123456 should return
const finalNumber = 162534
or
const initialNumber2 = 104 should return
const finalNumber2 = 140
Also, the number should be between the values of 0 and 100.000.000.
How to do it? Should I first transform the number into an array first of all using the split() method and then using a for loop?
You can do it with splitting the input into array and reduce:
const shuffle = input => input.toString().split('').reduce((acc, item, index, data) => {
const arr = (index % 2 ? data.slice().reverse() : data);
return acc.concat(arr[Math.floor(index / 2)]);
}, []).join('');
console.log(shuffle(104)); // 140
console.log(shuffle(123456)); // 162534
console.log(shuffle(1234567)); // 1726354
A bit reduced code:
const shuffle = input => input.toString().split('').reduce((acc, item, index, data) =>
acc.concat((index % 2 ? data.slice().reverse() : data)[Math.floor(index / 2)]), []
).join('');
I would prefer converting the number to a string, then using that string in a for loop.
function shuffle(num) {
var str = num.toString();
var result = "";
if(!isNaN(num) && num >= 0 && num <= 100000000) {
for(var i = 0; i < str.length; i++) {
if(i % 2 == 0) {
result += str[Math.floor(i / 2)];
} else {
result += str[str.length - Math.floor(i / 2 + 1)];
}
}
}
return result;
}
console.log(shuffle(123456)); // 162534
console.log(shuffle(1234567)); // 1726354
console.log(shuffle(104)); // 140
This may work as a very basic algo.
function shuffleNum(num,i){
var numArr = num.toString().split('');
var front = numArr.splice(0,i);
var back = numArr.pop();
var shuffledArr = front.concat(back,numArr);
return parseFloat(shuffledArr.join(''));
}
// Test
var num = 12345;
for(var i=0;i<num.toString().length;i++){
num = shuffleNum(num);
console.log(num);
}
// Output
// 51234
// 45123
// 34512
// 23451
// 12345
The best way is to use array push function.
function shuffle(a) {
var b = a.toString();
var c = [];
for(let i=0; i<b.length/2; i++) {
c.push(b[i]);
if(i==(Math.ceil(b.length/2)-1) && b.length%2==1) continue;
c.push(b[b.length-i-1]);
}
return c.join("");
}
Java code snippet.
public static int solution(int a) {
int[] arr = Integer.toString(a).chars().map(e->e-'0').toArray();
System.out.println(Arrays.toString(arr));
int[] temp = new int[arr.length];
int j=1;
for(int i = 0; i<arr.length; i++) {
if(i % 2 == 0) {
temp[i] = arr[i/2];
} else {
temp[i] = arr[arr.length - j];
j++;
}
}
System.out.println(Arrays.toString(temp));
Can't seem to figure out why I keep printing out extra 0's.
As of now, if the value was 730, this is what shows up:
Expected: '700 + 30', instead got: '700 + 30 + 0'
Criteria:
"You will be given a number and you will need to return it as a string in Expanded Form. For example:
12 Should return 10 + 2
42 Should return 40 + 2
70304 Should return 70000 + 300 + 4
NOTE: All numbers will be whole numbers greater than 0."
function expandedForm(num) {
var i,
position,
numArr = Array.from(num.toString()).map(Number),
numArrLen = numArr.length,
result = '';
if(num < 10){
return num;
} else {
for(i = 0; i < numArrLen; i++){
position = numArrLen-1-i;
if( i === numArrLen-1 ){
result += numArr[i] * Math.pow(10, position);
console.log('done',result);
} else {
if(numArr[i] !== 0){
result += numArr[i] * Math.pow(10, position) + " + ";
console.log('keep going',result);
} else {
continue;
console.log('zero', result);
}
}
}
return result;
}
}
Well it goes into the first if check which does not account for zero so you need to check if it is zero in that check.
if (i === numArrLen-1 && numArr[i]!==0)
And the issue you will have after that is you will have a trailing +.
What I would do is instead of adding the string, I would push to an array and join
var result = []
result.push(30)
result.push(2)
console.log(result.join(" + ")
and you can use array methods to actually solve it.
(102030).toString().split("").map((n,i,a) => n*Math.pow(10, a.length-i-1)).filter(n => n>0).join(" + ")
This is how I solved the solution.
function expandedForm(num) {
// Convert num to a string array
let numStringArray = Array.from(String(num));
// Get length of string array
let len = numStringArray.length;
let result = '';
// For each digit in array
numStringArray.map( (n,index) => {
// Perform only if n > 0
if( n>0 ) {
// Add plus sign if result is not empty (for the next digits)
if( result ) { result += ' + '; };
// Pad zeros the right limited to array length minus current index
result += new String(n).padEnd(len-index,'0');
}
});
return result;
}
This was my solution, could have been refactored better but it works.
function expandedForm(num) {
let myStr = num.toString().split(''), //convert number to string array
strLength = myStr.length,
arr = new Array();
if (strLength === 1) { /*If array length is one*/
return num;
}
for (let i = 0; i < strLength; i++) { //loop
if (myStr[i] === 0) { //if number starts with 0
arr.push('0')
}
if (i + 1 === strLength) { //if array is close to end
arr.push(myStr[i])
}
// add zero to number by length difference in array
// add number to
if (myStr[i] !== 0 && i + 1 !== strLength) {
for (let x = 1; x < (strLength - myStr.indexOf(myStr[i])); x++) {
myStr[i] += '0'
}
arr.push(myStr[i]);
}
}
return arr.filter((obj) => obj.match(/[1-9]/)) //remove items with just 0s
.map(obj => obj += ' + ') // add '+'
.reduce((a, b) => a + b).slice(0, -2); //remove whitespace
}
function expandedForm(num) {
const arr = num
.toString()
.split('');
for (let i = 0; i < arr.length - 1; ++i) {
if (arr[i] > 0) {
for (let j = i; j < arr.length - 1; ++j) {
arr[i] += '0';
}
}
}
return arr
.join()
.replace(new RegExp(",0", "g"), "")
.replace(new RegExp(",", "g"), " + ");
}
My solution is fairly similar, uses a bit of RegExp at the end in order to strip out the commas and lone zeroes however
I have a function that returns the sum of all its digits For both POSITIVE and NEGATIVE numbers.
I used split method and converted it to string first and then used reduce to add them all. If the number is negative, the first digit should count as negative.
function sumDigits(num) {
var output = [],
sNum = num.toString();
for (var i = 0; i < sNum.length; i++) {
output.push(sNum[i]);
}
return output.reduce(function(total, item){
return Number(total) + Number(item);
});
}
var output = sumDigits(1148);
console.log(output); // --> MUST RETURN 14
var output2 = sumDigits(-316);
console.log(output2); // --> MUST RETURN 4
Instead of returning the sum, it returned 4592 -1264
Am I doing it right or do I need to use split function? Or is there any better way to do this?
Sorry newbie here.
I think you'll have to treat it as a string and check iterate over the string checking for a '-' and when you find one grab two characters and convert to an integer to push onto the array. Then loop over the array and sum them. Of course you could do that as you go and not bother pushing them on the array at all.
function sumDigits(num) {
num = num + '';
var output = [];
var tempNum;
var sum = 0;
for (var i = 0; i < num.length; i++) {
if (num[i] === '-') {
tempNum = num[i] + num[i + 1];
i++;
} else {
tempNum = num[i];
}
output.push(parseInt(tempNum, 10));
}
for (var j = 0; j < output.length; j++) {
sum = sum + output[j];
}
return sum;
}
var output = sumDigits(1148);
console.log(output); // --> MUST RETURN 14
var output2 = sumDigits(-316);
console.log(output2); // --> MUST RETURN 4