[英]Push values to array, and print values from array
I am trying to make js class where I will do following.我正在尝试制作 js 类,我将在其中执行以下操作。 Send car name (txt) with usages (number).
发送带有用法(编号)的汽车名称(txt)。 For two kinds of cars I have some calculations then I need to store those values in array.
对于两种汽车,我有一些计算,然后我需要将这些值存储在数组中。 After I store in array I need to do it again and store in same array.
在我存储在数组中后,我需要再次执行并存储在同一个数组中。 Once I complete I need to print data out of array with usages desc.
完成后,我需要使用用法说明从数组中打印数据。 I don't know if my logic is ok, as I am starting learning JS some advices or class solution will me a lot.
我不知道我的逻辑是否正常,因为我正在开始学习 JS 一些建议或课堂解决方案对我来说很多。
<body>
<input type="text" id="name">
<input type="text" id="vrijednost">
<button type="button" onclick="submit()">Submit</button>
<p id="demo"><p>
</body>
<script src="cons.js"></script>
<script>
function submit(){
name = document.getElementById('name').value;
vrijednost = document.getElementById('vrijednost').value;
test = new CarUsage(name, vrijednost)
console.log(test.get());
}
</script>
class CarUsage{
constructor(total, car){}
calCarUsage(name, usage){
let car = {
carName: name,
carUsage: usage
};
if(car.carName=='Mercedes'){
this.car = "Mercedes";
this.total = 20 + usage * 10;
return store(this.car, this.total)
}
else if(car.carName==='Golf'){
this.car = "Golf";
this.total = 800;
return store(this.car, this.total)
}
else{
this.car === "Other";
this.total === '160' ;
return store(this.car, this.total)
}
}
store(name, usage){
var usages = {carName:name, usage:usage};
}
get(){
for(i=0;i<usages.length;i++){
alert("Car:- " + usages[i].carName + "Usage:- " + usages[i].usage);
}
}
}
I don't understand what your trying to achieve here: But regarding arrays and how you add items and print its values, heres one way of achieving it:我不明白你想在这里实现什么:但是关于数组以及如何添加项目和打印其值,这是实现它的一种方法:
var sentence = []
var words = ['this', 'is', 'one', 'way', 'of', 'push', 'to', 'an', 'array','and','print', 'the', 'values']
for(var i = 0; i < words.length; i++){
sentence.push(words[i]);
}
console.log("Here's my sentence: " + sentence.join()) // => Here's my sentence: this is one way of pushing to an array and print the values
or if you simply want to print each item in the array:或者如果您只是想打印数组中的每个项目:
for(var i = 0; i < sentence.length; i++){
console.log(sentence[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.