+ 1
JavaScript store manager code coach
Please how do I solve this code coach problem on JavaScript I have tried this it works but the results is saved in a single array please is there a way I can combine all results in one array? What I did: function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var i; for(i=0;i<4;i++){ var n = [prices[i] + increase]; //var result = new Array(n); console.log(n); } }
38 Réponses
+ 41
This is how I did it. First I created empty array. Then I looped through prices array and in every loop I used array push method to add increased number to the new array. I think it is better to put prices.length than a fixed number (in this case 4) in a loop. Since we are all mostly beginners the most important thing is that it solves the task, but we should try to make our code as conventional and reusable as possible.
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var newPrices = [];
for( i = 0; i < prices.length; i++){
newPrices.push(prices[i] + increase);
}
console.log(newPrices)
}
+ 15
Easiest way to solve this code challenge.......
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
for (i=0; i<4; i++){
prices[i] = increase + prices[i];
}
console.log(prices)
}
+ 9
I would like to add that it is better to put length as a separate variable, then put it in the loop. This is mainly for performance, since loop will check every time the length of the array. If we premake that number as a variable, we save some processing time.
+ 5
I basically created an empty array, i used for each to loop through every value while also saving it in the new array with the increment. trying to keep as short and simple as possible. Like my professor says. "KEEP IT SIMPLE STUPID" lol
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var array=[];
prices.forEach(function(item) {
array.push(item+increase);
})
console.log('[ '+array.join(', ')+' ]');
}
+ 3
kimmy For example, this practise is called store manager. In this case there are only 4 prices/items, but I think most of the stores have a lot more than 4 items. To answer your questions: no, loops aren't necessary, you can hardcode everything, but imagine hardcoding 2000 different store items and their prices, and then few days later there is a sales action with 10% off on prices, and than you have to manually change all the prices. Loops are there to do that for you and make your life a lot easier.
+ 2
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var n = [];
for( i = 0; i < prices.length; i++){
n[i]=(prices[i] + increase);
}
console.log(n)
;
}
+ 2
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
for (i=0; i< prices.length; i++){
prices[i] = increase + prices[i];
}
console.log(prices)
}
+ 2
Here is how I found it
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
for(i=0;i<prices.length;i++);{
prices[0]+=increase;
prices[1]+=increase;
prices[2]+=increase;
prices[3]+=increase;
console.log(prices);
}
}
+ 1
Simpler way to do it.
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var newArr = [];
prices.forEach( (item)=>{
newArr.push(item + increase);
})
console.log(newArr);
}
forEach loop does not change values of an array, so you create a new empty array and use the push method to add items in it after looping through the prices array.
+ 1
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
count=0;
while (count<prices.length)
{
prices[count] = prices[count] + increase;
count++;
}
console.log(prices);
}
0
This worked for me:
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
for (i = 0; i < 4; i++) {
prices[i] += increase;
}
console.log(prices);
}
0
//for loop all the element+increase
for (i=0; i<prices.length; i++){
prices[i] = increase + prices[i];
}
console.log(prices)
0
is a loop necessary? what's the difference between using loop and no loop?
0
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
for (i=0; i<4; i++){
prices[i] = increase + prices[i];
}
console.log(prices)
}
0
I used while loop to do this, also with help from everyone here :)
//your code goes here
var i = 0;
while (i < 4){
prices[i] =prices[i]+ increase
i++
}
console.log(prices)
0
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026] ;
for(i=0; i< prices.length ; i++) {
prices[i] = increase + prices [i] ;
}
console.log(prices) ;
}
0
Hello everyone,
This is how i did it. The result is correct but i 've seen here that you have done it differently. I guess its like maths :)
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
for (i=0;i<4;i++){
prices[i]+=increase;
}
console.log(prices)
}
0
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
for (var i = 0; i < prices.length; i++){
prices[i] = prices[i] + increase
}
console.log(prices)
}
0
My work
for(i=0;i<prices.lengh;i++){
Console.log(prices[i] + increase)
}
0
Best way to solve this , more understandable
function main() {
var w= parseInt(readLine(), 10);
var a= [98.99, 15.2, 20, 1026];
//your code goes here
var b = a[0]+w
var c = a[1]+w
var d = a[2]+w
var e = a[3]+w
console.log(new Array(b,c,d,e));
}