0
Store Manager
You are working on a Store Manager program, which stores the prices in an array. You need to add functionality to increase the prices by the given amount. The increase variable is taken from user input. You need to increase all the prices in the given array by that amount and output to the console the resulting array. _____&_____ mycode _____&_____ function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here }
6 Respostas
+ 3
function main()
{
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
var sum = [];
for(i = 0; i<prices.length; i++)
{
sum[i] = (prices[i] + increase);
}
console.log(sum)
}
+ 1
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
for(var i=0;i<prices.length ;i++){
prices[i]+=increase;
}
console.log(prices);
}
0
My solution:
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
prices[0] = 98.99 + increase;
prices[1] = 15.2 + increase;
prices[2] = 20 + increase;
prices[3] = 1026 + increase;
console.log(prices);
}
0
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var pricesIn = prices.map(ajout)
function ajout (value){
return value + increase ;
}
console.log(pricesIn)
}
- 1
Use javascript map for that: const prices = [98.99, 15.2,20, 1026].map(price => price+increase);
or copy the array into a new one: const newPrices = [...prices].map(price => price+increase);
- 2
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var pl = prices.length;
for(i=0; i<pl;i++){
prices[i]+=increase
}
console.log(prices)
}