Skip to main content
Code Review

Return to Question

edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

There are no explanations. You have to create the code that gives the following results:


oneTwoThree(0) ---> ['0', '0']
oneTwoThree(1) ---> ['1', '1']
oneTwoThree(3) ---> ['3', '111']
oneTwoThree(19) ---> ['991', '1111111111111111111']

There are no explanations. You have to create the code that gives the following results:

oneTwoThree(0)['0', '0']
oneTwoThree(1)['1', '1']
oneTwoThree(3)['3', '111']
oneTwoThree(19)['991', '1111111111111111111']

I have solved the challenge but it appears to be quite slow and iI was hoping to get some constructive ideas on how to improve the speed and efficacy of this code thanks.

function oneTwoThree(n) {
 var finalOutput = ['', ''],
 slotsArray = [],
 sum = 0,
 currentSlot = 0,
 slots = 1;
 /*
 My Idea was to create an array and the sum of that array will be equal to the parameter given "n". The array will have slots and the number of slots is unknown because the parameter "n" is arbitrary. each slot will contain a number from 9-1, we will start out with 9 because it is the highest, I will add all the slots and the sum must be equal to parameter "n" thus will give us the first item in our final array.
 */
 if (n >= 10) {
 while (!(9 * slots >= n)) { // check how many slots are needed
 slots += 1;
 }
 while (slotsArray.length != slots) {
 // place a 9 in every slot, starting with the highest numbern this should give us something like "[9,9,9]" for examply
 slotsArray.push(9);
 }
 // this is how I will keep track of what slot I am currently in and we will start from right to left.
 currentSlot = slotsArray.length - 1;
 for (var i = 0; i < slotsArray.length; i++) {
 //here I test if the sum of the array is equal to "n"
 sum += slotsArray[i];
 }
 // if it is not equal we proceed here.
 while (sum != n) {
 slotsArray[currentSlot] -= 1; // subtract 1 from the current slot example: [9,9,8]
 // I check agian if the sum is equal to n
 for (var i = 0; i < slotsArray.length; i++) {
 sum += slotsArray[i];
 }
 // reset the sum to 0 if its not equal
 if (sum != n) {
 sum = 0
 }
 // This will continue until if finds the sum that equals "n"
 }
 // convert the array into a string for the final output
 for (var i = 0; i < slotsArray.length; i++) {
 finalOutput[0] += String(slotsArray[i]);
 }
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 // if the parameter "n" is less than 10 
 if (n < 10) {
 finalOutput[0] = String(n);
 if (n == 0) {
 finalOutput[1] += String(n);
 } else {
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 }
 console.log(finalOutput);
 return finalOutput;
}
oneTwoThree(19);
oneTwoThree(1);
oneTwoThree(7);
oneTwoThree(36);
oneTwoThree(0);
http://codereview.stackexchange.com/questions/ask#

There are no explanations. You have to create the code that gives the following results:


oneTwoThree(0) ---> ['0', '0']
oneTwoThree(1) ---> ['1', '1']
oneTwoThree(3) ---> ['3', '111']
oneTwoThree(19) ---> ['991', '1111111111111111111']

I have solved the challenge but it appears to be quite slow and i was hoping to get some constructive ideas on how to improve the speed and efficacy of this code thanks.

function oneTwoThree(n) {
 var finalOutput = ['', ''],
 slotsArray = [],
 sum = 0,
 currentSlot = 0,
 slots = 1;
 /*
 My Idea was to create an array and the sum of that array will be equal to the parameter given "n". The array will have slots and the number of slots is unknown because the parameter "n" is arbitrary. each slot will contain a number from 9-1, we will start out with 9 because it is the highest, I will add all the slots and the sum must be equal to parameter "n" thus will give us the first item in our final array.
 */
 if (n >= 10) {
 while (!(9 * slots >= n)) { // check how many slots are needed
 slots += 1;
 }
 while (slotsArray.length != slots) {
 // place a 9 in every slot, starting with the highest numbern this should give us something like "[9,9,9]" for examply
 slotsArray.push(9);
 }
 // this is how I will keep track of what slot I am currently in and we will start from right to left.
 currentSlot = slotsArray.length - 1;
 for (var i = 0; i < slotsArray.length; i++) {
 //here I test if the sum of the array is equal to "n"
 sum += slotsArray[i];
 }
 // if it is not equal we proceed here.
 while (sum != n) {
 slotsArray[currentSlot] -= 1; // subtract 1 from the current slot example: [9,9,8]
 // I check agian if the sum is equal to n
 for (var i = 0; i < slotsArray.length; i++) {
 sum += slotsArray[i];
 }
 // reset the sum to 0 if its not equal
 if (sum != n) {
 sum = 0
 }
 // This will continue until if finds the sum that equals "n"
 }
 // convert the array into a string for the final output
 for (var i = 0; i < slotsArray.length; i++) {
 finalOutput[0] += String(slotsArray[i]);
 }
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 // if the parameter "n" is less than 10 
 if (n < 10) {
 finalOutput[0] = String(n);
 if (n == 0) {
 finalOutput[1] += String(n);
 } else {
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 }
 console.log(finalOutput);
 return finalOutput;
}
oneTwoThree(19);
oneTwoThree(1);
oneTwoThree(7);
oneTwoThree(36);
oneTwoThree(0);
http://codereview.stackexchange.com/questions/ask#

There are no explanations. You have to create the code that gives the following results:

oneTwoThree(0)['0', '0']
oneTwoThree(1)['1', '1']
oneTwoThree(3)['3', '111']
oneTwoThree(19)['991', '1111111111111111111']

I have solved the challenge but it appears to be quite slow and I was hoping to get some constructive ideas on how to improve the speed and efficacy of this code thanks.

function oneTwoThree(n) {
 var finalOutput = ['', ''],
 slotsArray = [],
 sum = 0,
 currentSlot = 0,
 slots = 1;
 /*
 My Idea was to create an array and the sum of that array will be equal to the parameter given "n". The array will have slots and the number of slots is unknown because the parameter "n" is arbitrary. each slot will contain a number from 9-1, we will start out with 9 because it is the highest, I will add all the slots and the sum must be equal to parameter "n" thus will give us the first item in our final array.
 */
 if (n >= 10) {
 while (!(9 * slots >= n)) { // check how many slots are needed
 slots += 1;
 }
 while (slotsArray.length != slots) {
 // place a 9 in every slot, starting with the highest numbern this should give us something like "[9,9,9]" for examply
 slotsArray.push(9);
 }
 // this is how I will keep track of what slot I am currently in and we will start from right to left.
 currentSlot = slotsArray.length - 1;
 for (var i = 0; i < slotsArray.length; i++) {
 //here I test if the sum of the array is equal to "n"
 sum += slotsArray[i];
 }
 // if it is not equal we proceed here.
 while (sum != n) {
 slotsArray[currentSlot] -= 1; // subtract 1 from the current slot example: [9,9,8]
 // I check agian if the sum is equal to n
 for (var i = 0; i < slotsArray.length; i++) {
 sum += slotsArray[i];
 }
 // reset the sum to 0 if its not equal
 if (sum != n) {
 sum = 0
 }
 // This will continue until if finds the sum that equals "n"
 }
 // convert the array into a string for the final output
 for (var i = 0; i < slotsArray.length; i++) {
 finalOutput[0] += String(slotsArray[i]);
 }
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 // if the parameter "n" is less than 10 
 if (n < 10) {
 finalOutput[0] = String(n);
 if (n == 0) {
 finalOutput[1] += String(n);
 } else {
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 }
 console.log(finalOutput);
 return finalOutput;
}
oneTwoThree(19);
oneTwoThree(1);
oneTwoThree(7);
oneTwoThree(36);
oneTwoThree(0);
Source Link

Codewars Challenge Kata

I was recently doing a challenge on Codewars and this is what challenge said:

There are no explanations. You have to create the code that gives the following results:


oneTwoThree(0) ---> ['0', '0']
oneTwoThree(1) ---> ['1', '1']
oneTwoThree(3) ---> ['3', '111']
oneTwoThree(19) ---> ['991', '1111111111111111111']


I have solved the challenge but it appears to be quite slow and i was hoping to get some constructive ideas on how to improve the speed and efficacy of this code thanks.

function oneTwoThree(n) {
 var finalOutput = ['', ''],
 slotsArray = [],
 sum = 0,
 currentSlot = 0,
 slots = 1;
 /*
 My Idea was to create an array and the sum of that array will be equal to the parameter given "n". The array will have slots and the number of slots is unknown because the parameter "n" is arbitrary. each slot will contain a number from 9-1, we will start out with 9 because it is the highest, I will add all the slots and the sum must be equal to parameter "n" thus will give us the first item in our final array.
 */
 if (n >= 10) {
 while (!(9 * slots >= n)) { // check how many slots are needed
 slots += 1;
 }
 while (slotsArray.length != slots) {
 // place a 9 in every slot, starting with the highest numbern this should give us something like "[9,9,9]" for examply
 slotsArray.push(9);
 }
 // this is how I will keep track of what slot I am currently in and we will start from right to left.
 currentSlot = slotsArray.length - 1;
 for (var i = 0; i < slotsArray.length; i++) {
 //here I test if the sum of the array is equal to "n"
 sum += slotsArray[i];
 }
 // if it is not equal we proceed here.
 while (sum != n) {
 slotsArray[currentSlot] -= 1; // subtract 1 from the current slot example: [9,9,8]
 // I check agian if the sum is equal to n
 for (var i = 0; i < slotsArray.length; i++) {
 sum += slotsArray[i];
 }
 // reset the sum to 0 if its not equal
 if (sum != n) {
 sum = 0
 }
 // This will continue until if finds the sum that equals "n"
 }
 // convert the array into a string for the final output
 for (var i = 0; i < slotsArray.length; i++) {
 finalOutput[0] += String(slotsArray[i]);
 }
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 // if the parameter "n" is less than 10 
 if (n < 10) {
 finalOutput[0] = String(n);
 if (n == 0) {
 finalOutput[1] += String(n);
 } else {
 while (finalOutput[1].length != n) {
 finalOutput[1] += '1';
 }
 }
 }
 console.log(finalOutput);
 return finalOutput;
}
oneTwoThree(19);
oneTwoThree(1);
oneTwoThree(7);
oneTwoThree(36);
oneTwoThree(0);
http://codereview.stackexchange.com/questions/ask#

default

AltStyle によって変換されたページ (->オリジナル) /