Replacing console.log with document.getElementById("ID") for loops [duplicate] - javascript

This question already has answers here:
For loop through Array only shows last value
(2 answers)
Closed 12 months ago.
// program to display text 10 times
const n = 10;
// looping from i = 1 to 10
for (let i = 1; i <= n; i++) {
console.log(`I love JavaScript.`);
}
I try to replace console.log with document.getElementById("demo").innerHTML = "I love JavaScript", but can't replicate the example.

You have to use += "I love JavaScript."

Related

I'm trying to create a function that follows the Luhn's Algorithm [duplicate]

This question already has answers here:
Implementation of Luhn algorithm
(14 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I tried to multiply each number whose index number is an even number by two, and that worked fine. Still, the problem lies here: If any of the results is greater than or equal to 10, then add up the two numbers, for example, if one of the results is 12, then add up 1 and 2, which should be equal to 3. So this is what I tried:
var num = 122345643345673;
var convNum = num.toString();
var aftertoString = convNum.split("");
for(let i = 1; i < aftertoString.length; i++){
if (i % 2 == 0) {
var multi = aftertoString[i] * 2;
if(multi > 10){
var multiStringed = multi.toString();
var aftermutliStringed = multiStringed.split("");
var first = parseInt(aftermutliStringed[2])
var multi = first + first;
}
console.log(multi);
}
}
Since any index of the "aftermultiSringed" array is not a number, I tried to convert it to a number using the "parseInt()" method, but the result is NaN, please why am I getting this.
The method parseInt usage is incorrect.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
// var first = aftermultiStringed[1].parseInt();
var first = parseInt(aftermultiStringed[1]);

Filling in undefined value in javascript object

I'm working on a problem where the task is to write a program which reconstructs each sentence out of a set of words and prints out the original sentences.
INPUT SAMPLE:
2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2
And the answer is:
However, it was not implemented until 1998 and 2000
So far I got to the point where I have combined the words and number hints together as a pair value in an object. The only problem I am running into is that there is actually a missing number hint, thus one of the words has an undefined value.
How can I fill in this value?
I have tried to use .HasOwnProperty() and for-looping through to see if one of the values equals to undefined, but neither has worked. Any input would be greatly appreciated!
function encyrption(str){
var string = str.split(";");
var words = string[0].split(" ");
var hints = string[1].split(" ");
var object = {};
for(var i = 0; i < words.length; i++){
if(object[hints[i]] === undefined){
/////???
}else
object[hints[i]] = words[i];
}
return object;
}
console.info(encyrption("2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2"));
I'd do something like that, just guessing that the missing hint is the last word, and that will always be the sixth position. If that's not the case I'd need more information about the problem test cases to solve it.
function encyrption(str){
var string = str.split(";");
var words = string[0].split(" ");
var hints = string[1].split(" ");
var hints_sorted = hints.concat().sort();
var missing_hint;
var object = {};
for(var i = 0; i < words.length; i++) {
if(hints_sorted[i] != i+1) {
missing_hint = (i+1).toString();
break;
}
}
hints.push(missing_hint);
for(var i = 0; i < words.length; i++){
object[hints[i]] = words[i];
}
return object;
}
console.info(encyrption("2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2"));
//Result: However, it was not implemented until 1998 and 2000
There you have a small explanation:
I created the hints_sorted array, which is a copy of the hints one, but sorted, so, in our example:
hints = ['9','8','3','4','1','5','7','2'];
hints_sorted = ['1','2','3','4','5','7','8','9'];
Then, inside the for, I'm comparing the value with the index + 1 (since the index inside the loop starts at zero):
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
7 -> 6
On the sixth element, we have 7 on our array and we are expecting 6, so it goes inside the if, we set 6 as our missing hint, and we break; the loop so it doesn't continue checking values.

Manipulating a String's content with an Integer [duplicate]

This question already has answers here:
Repeat String - Javascript [duplicate]
(30 answers)
Closed 6 years ago.
I'f you're familiar with Python, I'm sure you're aware that you can multiply a String by an Integer to get that desired amount of Strings back.
Example:
'J' * 3 --> 'JJJ'
What's the most efficient way to do this in JavaScript?
I was looking for an inline method; similar to that of Pythons behaviour
A few of my ideas:
var str = 'J',
strOld = str,
timesToExtend = 3;
for(var i = 0; i < timesToExtend; i++){
str += strOld;
}
= 'JJJ'
var str = 'J',
timesToExtend = 5,
strOld = str;
while(!timesToExtend){
str += strOld;
timesToExtend--;
}
These are just rough ideas, so don't expect them to be 100% accurate and working. I assume this MUST contain a loop; however any method without a loop will be praised!
Thanks for reading; thank you for your response in advance!
In ES6 you can use Array.prototype.fill() and then join the array:
Array(3).fill('J').join('');

for loop with chars java script [duplicate]

This question already has answers here:
How can I display letters using html table cells as colored pixels?
(2 answers)
Closed 7 years ago.
Quick question. I am trying to give out numbers with a for loop in JavaScript.
Unfortunately this is not working, if I would code in java then the solution would be replace var with char and cosole.log with println and got it, but hereā€¦ do you have a solution for that ?
for ( var i = 'a'; i < 'z'; i++) {
console.log(i);
}
for (var i = 'a'; i !== nextChar('z'); i = nextChar(i)) {
console.log(i);
}
function nextChar(c) {
return String.fromCharCode(c.charCodeAt(0) + 1);
}

Bit Pattern in JavaScript [duplicate]

This question already has answers here:
String Conversion in Javascript (Decimal to Binary)
(5 answers)
Closed 7 years ago.
I would like to ask help whether am I doing the right thing or not. You see I am trying to test myself by displaying the bit pattern of a number in the most efficient way as possible. But I'm having trouble on how to display the pattern cause I'm still learning javascript. Here's my code.
<script>
var bitPattern = function(given) {
for(var i = 1 << 31; i > 0; i = i / 2){
document.write((given & i) ? 1 : 0);
}
};
var number = prompt("Enter a number to convert: ");
bitPattern(number);
</script>
The best way to do this is:
var number = prompt("Enter a number to convert: ");
var bitPattern = parseInt(number).toString(2);
document.write(bitPattern);

Categories